Javascript Match and RegExp Issue — Strange Behavior?

This is not unusual behaviour at all. In regex 1 you are only checking for 1 instance of it where in regex 3 you have told it to return all instances of the item by using the /gi argument In Regex 2 you are assuming that "/b/" === /b/ when it doesn't. "/b/"!

== /b/. "/b/" is a string that is searching so if you string has "/b/" in it then it will return while /b/ means that it needs to search between the slashes so you could have "abc" and it will return "b I hope that helps EDIT: Looking into it a little bit more, the exec methods returns the first match that it finds rather than all the matches that it finds EDIT: var myRe = /ab*/g; var str = "abbcdefabh"; var myArray; while ((myArray = myRe. Exec(str))!

= null) { var msg = "Found " + myArray0 + ". "; msg += "Next match starts at " + myRe. LastIndex; console.

Log(msg); } Having a look at it again it definitely does return the first instance that it finds. If you looped through it then would return more Why it does this? I have no idea...my JavaScript Kung Fu clearly isnt strong enough to answer that part.

This is not unusual behaviour at all. In regex 1 you are only checking for 1 instance of it where in regex 3 you have told it to return all instances of the item by using the /gi argument. In Regex 2 you are assuming that "/b/" === /b/ when it doesn't."/b/"!

== /b/. "/b/" is a string that is searching so if you string has "/b/" in it then it will return while /b/ means that it needs to search between the slashes so you could have "abc" and it will return "b" I hope that helps. EDIT: Looking into it a little bit more, the exec methods returns the first match that it finds rather than all the matches that it finds.

EDIT: var myRe = /ab*/g; var str = "abbcdefabh"; var myArray; while ((myArray = myRe. Exec(str))! = null) { var msg = "Found " + myArray0 + ".

"; msg += "Next match starts at " + myRe. LastIndex; console. Log(msg); } Having a look at it again it definitely does return the first instance that it finds.

If you looped through it then would return more. Why it does this? I have no idea...my JavaScript Kung Fu clearly isnt strong enough to answer that part.

I should have made it clear. For the regx1, it doesn't make a difference when I added "gi" to the definition of the RegExp object. Try it yourself.

– picardo Jul 16 '09 at 11:42 Do you know why it does that? I did add the g flag in the RegExp definition, so it should do a global match, right? – picardo Jul 16 '09 at 11:53.

The reason regex 2 is returning null is that you're passing "/b/" as the pattern parameter, while "b" is actually the only thing that is actually part of the pattern. The slashes are shorthand for regex, just as is for array. So if you were to replace that to just new regex("b"), you'd get one match, but only one, since you're omitting the "global+ignorecase" flags in that example.To get the same results for #2 and #3, modify accordingly: var regex2 = stringLowerCase; console.

Log("regex 2: ", a.text. Match(regex2, "gi")); console. Log("regex 3: ", a.text.

Match(/b/gi)).

Regex2 is a string, not a RegExp, I had trouble too using this kind of syntax, tho i'm not really sure of the behavior. Edit : Remebered : for regex2, JS looks for "/b/" as a needle, not "b".

That should have worked. How did you end up solving your trouble? – picardo Jul 16 '09 at 11:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions