As a JavaScript novice, I recently delved into the world of regular expressions for the first time.
After attempting to perform some matching operations, I found myself puzzled by the results.
My goal was simple: I wanted to match every website name in the following sentence:
"I go to google.com to search, to facebook.com to share and to yahoo.com to send an email."
This is the code I used:
var text = "I go to google.com to search, to facebook.com to share and to yahoo.com to send an email.";
var pattern = /\w+\.\w+/g;
var matches = pattern.exec(text);
document.write("matches index : " + matches.index + "<br>");
document.write("matches input : " + matches.input + "<br>");
document.write("<br>");
for(i=0 ; i<matches.length ; i++){
document.write("match number " + i + " : " + matches[i] + "<br>");
}
Here's what I got as a result:
matches index : 0
matches input : i go to google.com to search, to facebook.com to share and to yahoo.com to send an email
match number 0 : google.com
I wondered why it only matched 'google.com' and not the other websites. Can anyone explain?