regex - How to match a substring surrounded by known prefix and suffix in javascript -
given string, such as:
example string intended nested string match.
how isolate substring knowing prefix , suffix it, e.g. between intended
, to match
?
use regular expressions non-capturing parentheses, so:
string = 'example string intended nested string match.'; regexp = /(?:intended)(.*)(?:to match)/; firstmatch = regexp.exec(string)[1]; // " nested string "
the question mark has several uses in regular expressions, parentheses question mark colon form (?:
more regex)
non-capturing parentheses.
see mdn more details of exec(), string.match(), , regular expressions.
Comments
Post a Comment