javascript - Can't get RegExp to match in GWT -
example text: in park, kid plays. kid tall. kid watches kid @ play.
i want match "child" in first sentence, "child" in sec , 3rd sentences not "child" in 3rd sentence. or in other words, match "child" or "child" not if proceeded word "another"
i thought using negative behind
((?<\!another) [cc]hild)
but can't seem syntax right produce valid regexp.
even if syntax right not sure can in gwt. here snippet gwt javadoc
java-specific constructs in regular look syntax (e.g. [a-z&&[^bc]], (?<=foo), \a, \q)
work in pure java implementation, not gwt implementation,...
any help or insight appreciated.
update:
colin's reply works isn't quite right.
colin's regex match "child" , "child" , not match "another child" asked. there few problems though.
what trying match on "child" , "child" can replaced either child's name or right pronoun he/she, depending on child's gender.
the problem colin's regex matches ", child" , ". child". doesn't match "child" if first word in text. example:
"child went park. in park, kid plays. kid tall. kid watches kid @ play."
the first kid not match. subsequent matches on ", child", ". child", , ". child".
i worked on regex colin came trying match "child" or "child" can't create work.
the regex in gwt has same level of back upwards regexp javascript, since calls on native javascript classes.
i can't think of way reject "another child" straight in regex, given javascript regex doesn't have back upwards look-behind or possessive quantifier.
therefore, write regex that, if "another" appears before "child", "another" matched; otherwise, "child" matched. can filter out matches have more 5 characters.
regexp.compile("(?:another +)?[cc]hild", "g")
note "child" in string "some children" matched. , if "another" embedded within longer word string, illustration "ranother"1, blindly pick fragment. prevent such cases, need add together word boundary check \b
2:
regexp.compile("(?:\\banother +)?\\b[cc]hild\\b", "g") --- --- --- | | | prevent "ranother" prevent "children" matching or "nochild" matching
you may allow case-insensitive matching (which quite reasonable text) i
flag. however, leave decide.
using regex above, match "another child" before matching "child". therefore, when match contains "child", know "another" does not precede it. therefore, can filter away matches length > 5, , left valid strings.
footnote
i utilize made word example. normal in arbitrary string, don't know if there word in english language "another" embedded inside.
there caveat here. "child4" or "child_something" not matched when \b
used. while "another" in "_another child" or "5another child" not picked regex (and "child" matched, means take match). possible workaround this, , if request it.
javascript regex gwt negative-lookbehind