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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -