Skip to content Skip to sidebar Skip to footer

Regular Expression Doesn't Work Correctly In Javascript?

In an earlier thread about inserting brackets around 'comments' in a chess pgn-like string, I got excellent help finishing a regex that matches move lists and comments separately.

Solution 1:

I corrected your javascript code and got the following:

http://jsfiddle.net/ZXG2H/

  1. Personally I think the matching (group) problems are related to http://regex101.com/. Your expression works definitly in JavaScript (see the fiddle) and in Java (with escaping corrections). I minimalized your JavaScript slightly and used the pgn data from a parameter not a text input.

  2. I am not aware that =~ is available in JavaScript, but maybe I am wrong. Using JavaScript you loop through the matches using something like: (Why does it not format like code???)

    pattern=/myregexp/; while ((match=pattern.exec(mytext))!=null) { //do something }

  3. If no match is found for a group it returns null. You adress the groups by using the match variable from above with an index like match[2] is matching group 2.

Post a Comment for "Regular Expression Doesn't Work Correctly In Javascript?"