php - Regular Expression Doesn't Work Correctly in Javascript? -
in earlier thread inserting brackets around "comments" in chess pgn-like string, got excellent finishing regex matches move lists , comments separately.
here current regex:
((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[nbrqk]?[a-h1-8]?x?[a-ho][1-8-][o-]{0,3}[!?+#=]{0,2}[nbrq]?[!?+#]{0,2}(?:\s[nbrqk]?[a-h1-8]?x?[a-ho][1-8-][o-]{0,3}[!?+#=]{0,2}[nbrq]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[nbrqk]?[a-h1-8]?x?[a-ho][1-8-][o-]{0,3}[!?+#=]{0,2}[nbrq]?[!?+#]{0,2}).)+)
the 3 capture groups are:
- "e4 e5 2. f4 exf4 3.nf3" etc -- i.e. lists of moves
- "blah blah blah" -- i.e. "comments"
- comment ") (" comment -- i.e. close , begin parens, when chess variation comment @ end "completes", , chess variation comment @ beginning "starts"
in action here: http://regex101.com/r/dq9ly5
everything works correctly "your regular expression in" pcre(php): matches 3 groups correctly. when switch "your regular expression in" javascript, however, matches capture group 1. there in regex isn't supported javascript regex engine? tried research this, haven't been able solve it. there information on topic, , i've spent hours , hours.
i know 1 solution use regex as-is, , pass php through ajax, etc, don't know how yet (it's on list learn).
question 1: curious in regex doesn't work on javascript regex engine.
also, here javascript cleanpgntext
function. interested in while
, if else seems wrong, appreciate help.
function cleanpgntext(pgn) { var pgntextedited = ''; var str; var pgninputtextarea = document.getelementbyid("pgntextarea"); var pgnoutputarea = document.getelementbyid("pgnoutputtext"); str = pgninputtextarea.value; str = str.replace(/\[/g,"("); //sometimes uses [ incorrectly variations str = str.replace(/\]/g,")"); str = str.replace(/[\n¬]*/g,""); // remove newlines , weird character ms word sticks in str = str.replace(/\s{2,}/g," "); // turn more 1 space 1 space while ( str =~ /((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[nbrqk]?[a-h1-8]?x?[a-ho][1-8-][o-]{0,3}[!?+#=]{0,2}[nbrq]?[!?+#]{0,2}(?:\s[nbrqk]?[a-h1-8]?x?[a-ho][1-8-][o-]{0,3}[!?+#=]{0,2}[nbrq]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[nbrqk]?[a-h1-8]?x?[a-ho][1-8-][o-]{0,3}[!?+#=]{0,2}[nbrq]?[!?+#]{0,2})[^\)\(])+)|((?:\)\s\())/g ) { if ($1.length > 0) { // pgntextedited += $1; } else if ($2.length > 0) { pgntextedited += '{' + $2 + '}'; } else if ($3.length > 0) { pgntextedited += $3; } } pgnoutputarea.innerhtml = pgntextedited; }
question 2: regarding =~
in while
statement
while ( str =~
i got =~
helpful code in original thread, written in perl. don't quite understand how =~
operator works. can use same operator in javascript, or should using else?
question 3: can use .length way am, when say
if ($1.length > 0)
to see if first capture group had match?
thank in advance help. (if regex101 link doesn't work you, can sample pgn test on original thread).
i corrected javascript code , got following:
personally think matching (group) problems related http://regex101.com/. expression works definitly in javascript (see fiddle) , in java (with escaping corrections). minimalized javascript , used pgn data parameter not text input.
i not aware
=~
available in javascript, maybe wrong. using javascript loop through matches using like: (why not format code???)pattern=/myregexp/; while ((match=pattern.exec(mytext))!=null) { //do }
if no match found group returns
null
. adress groups usingmatch
variable above indexmatch[2]
matching group 2.
Comments
Post a Comment