繁体   English   中英

匹配正则表达式-匹配时如何获得两个字符串之间的差异

[英]match regex - How to get the difference between the two strings when matched

我执行一个字符串是否包含子字符串的测试,如果是,我希望子字符串不匹配

var string = "Hello world";
var toMatch = new RegExp('world', 'i');

if (string.match(toMatch)){
  // should return "Hello "
}

只需使用replace()

var string = "Hello world";
var toMatch = new RegExp('world', 'i');

var newString=string.replace(string.match(toMatch),"");

如果您想返回,请使用

return string.replace(string.match(toMatch),"");

如果包含“ world”,它将为您提供不包含“ world”的字符串;如果不包含“ world”,则将为您提供未修改的字符串

暂无
暂无

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

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