473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Regex assistance

I'm trying to extract several portions of a string and am having some issues.

The string is: "http://www.google.com http://www.yahoo.com http://www.facebook.com"

I would like to extract google, yahoo, and facebook from that string.

I've been trying this:
Expand|Select|Wrap|Line Numbers
  1. var string  = "http://www.google.com http://www.yahoo.com http://www.facebook.com";
  2. var several = string.match( /www.(\w+).com/g );
  3. console.log( several );
  4.  
This does not seem to be working. Can you please help me out?
Thanks!
Oct 20 '11 #1

✓ answered by Mariostg

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var haystack  = "http://www.google.com http://www.yahoo.com http://www.facebook.com";
  3. var urls=haystack.split(" ");
  4. var pattern=/www.(\w+).com/;
  5.  
  6. for (var i=0; i<urls.length;i++){
  7.     document.write(urls[i].match(pattern)[1]+'<br>');
  8. }
  9. </script>
Outputs:
google
yahoo
facebook

3 1360
johny10151981
1,059 1GB
If you are confident enough to read next string of "www." and before next . then do this

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT>
  2.     Query="www.yahoosadf.com";
  3.     n=Query.indexOf("www.",0)+4;
  4.     last=Query.indexOf(".",n);
  5.     document.write(Query.substring(n,last));
  6. </SCRIPT>
  7.  
Oct 20 '11 #2
gits
5,390 Expert Mod 4TB
you could even extend your regex with more excludes like this:
Expand|Select|Wrap|Line Numbers
  1. var s = "http://www.google.com http://www.yahoo.com http://www.facebook.com";
  2. var m = s.match(/[^www.|^com|^ http:\/\/](\w+)/g);
  3. console.log(m);
  4.  
Oct 20 '11 #3
Mariostg
332 100+
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var haystack  = "http://www.google.com http://www.yahoo.com http://www.facebook.com";
  3. var urls=haystack.split(" ");
  4. var pattern=/www.(\w+).com/;
  5.  
  6. for (var i=0; i<urls.length;i++){
  7.     document.write(urls[i].match(pattern)[1]+'<br>');
  8. }
  9. </script>
Outputs:
google
yahoo
facebook
Oct 20 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bob Dankert | last post by:
I am trying to create a regular expression to split a string at each newline, unless the newline is within a set of quotes. Unfortunately, I have little experience with Regular Expressions and...
5
by: Shaun Wilde | last post by:
When using the regular expression validator is there a way of failing a validation when you detect a match - I suppose a sort of anti-match. I want to detect certain things on user input and if...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
13
by: B. Williams | last post by:
I have written some code to accept input and place this input at the beginning or end of a list, but I need assistance including a class so that it will allow input of a phone number that is...
9
by: Greg Collins [Microsoft MVP] | last post by:
I've done a bit of research on this topic in the newsgroups and on MSDN, and though it sounds possible, I still don't understand how I would make it work. I want to be able to use named groups to...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: VUNETdotUS | last post by:
Hi, I am working with this regexp to extract address: city, state, and zip. This version kinda works but it extracts one element of an array instead of three and keeps my "city" too long, including...
1
by: DomoChan | last post by:
Given a repeatable group expression ()+ and given its input cab will result in nested subgroups, which taken from 'rad software regular expression tester' looks like
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.