我坚持一个条件:
替换特定序列,例如MA
,仅当MA
不在字母表之前或成功时才会显示空白字符串。
例如:
输入字符串:
string abc= "This is a MAin method with MA .Use your MA and MAt it "
输出字符串:
string result = "This is a MAin method with .Use your and MAt it "
注意:我写了MA
大写字母以便澄清。我们必须使用jQuery。
答案 0 :(得分:1)
你不需要jQuery,只需要一个简单的Javascript RegExp:
var input = "This is a MAin method with MA .Use your MA and MAt it ",
output = input.replace(/([^A-Za-z])(MA)([^A-Za-z])/g, '$1$3');
RegExp中的[^A-Za-z]
表示“不是字母字符”,就像你在问题中所说的那样。如果您还想包含数字,可以添加0-9
。如果您不关心MA
是大写还是小写,请改为使用/ig
结束您的RegExp。
答案 1 :(得分:1)
您应该可以使用MA\s([^a-zA-Z]+)
然后在javascript(不需要jquery)中执行以下操作:
var abc = 'This is a MAin method with MA .Use your MA and MAt it ';
var result = abc.replace(/MA\s*([^a-zA-Z]+)/g, '$1')
console.log('We want: This is a MAin method with .Use your and MAt it');
console.log('We got: ' + result);
您将获得以下输出:
We want: This is a MAin method with .Use your and MAt it
We got: This is a MAin method with .Use your and MAt it
所以空间太大,但你应该可以用一个简单的result = result.replace(' ', ' ');
替换它
答案 2 :(得分:0)
var input = "This is a MAin method with MA .Use your MA and MAt it ";
output = input.replace('MA ', '');