-3

我遇到了一种情况:

仅当不在字母表之前或不成功时,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 来完成。

4

3 回答 3

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是大写还是小写,则改为使用 RegExp 结尾/ig

于 2012-06-06T12:06:45.923 回答
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);

​http://jsfiddle.net/gjFCa/

您将获得以下输出:

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(' ', ' ');

于 2012-06-06T12:11:49.363 回答
0
var input = "This is a MAin method with MA .Use your MA and MAt it ";

output = input.replace('MA ', '');
于 2012-06-06T12:10:23.783 回答