简体   繁体   中英

Extract string between two strings

I have the following problem:

var previous_state = response.match(simulator.previousStateString+"(.*?),");             
if( ! previous_state ) {response.match(simulator.previousStateString+"(.*?) ")};
if( ! previous_state ) {response.match(simulator.previousStateString+"(.*?)#")};

Basically, I am looking for a string between simulator.previousStateString and ',' or ' ' or'#'. Is there the possibility to have a more compact code? I mean, can I put all 3 regex in just one?

Thanks

Sure you can use this regex with character class :

response.match( new RegExp(simulator.previousStateString + "(.*?)[, #]") ); 

It matches only one out of several characters inside [ and ]

PS: Make sure there is no special regex character in simulator.previousStateString

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM