如何通过正则表达式 match() 分隔字符串? 我只想使用 jQuery RegEx。
var MyStr = 'BeginStr ABCDEF EndStr' // The result should: ABCDEF
如何分离“ABCDEF”?
下面是一个解决方案,但我想改进它,如何消除函数replace()? 我只想使用一次 match() 函数。
var MyStr = 'BeginStr ABCDEF EndStr'; // The result should: ABCDEF
sRegEx = /BeginStr.*?(?=EndStr)/;
var sResult = String(MyStr.match(sRegEx)); // It results: BeginStr ABCDEF
var sMenuPoint = String(MyStr.match(sRegEx)).replace(/BeginStr/, ''); // It results: ABCDEF
alert(sResult);
提前致谢, 桑德罗。
最佳答案
使用简单的 replace()
函数以及组捕获($1
、$2
等)即可为您完成此操作:
sResult = MyStr.replace(/.*BeginStr(.*?)(?=EndStr).*/, "$1");
或
sResult = MyStr.replace(/.*BeginStr(.*?)EndStr.*/, "$1");
关于javascript - jquery regexp - 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392477/