简体   繁体   English

提取两个字符串之间的字符串

[英]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'#'. 基本上,我正在寻找Simulator.previousStateString和','或''或'#'之间的字符串。 Is there the possibility to have a more compact code? 是否有可能使用更紧凑的代码? I mean, can I put all 3 regex in just one? 我的意思是,我可以将所有3个正则表达式合而为一吗?

Thanks 谢谢

Sure you can use this regex with character class : 确保您可以将此正则表达式与character class

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

It matches only one out of several characters inside [ and ] 它只匹配[ and ]中几个字符中的一个

PS: Make sure there is no special regex character in simulator.previousStateString PS:确保simulator.previousStateString没有特殊的正则表达式字符simulator.previousStateString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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