当前位置: 代码迷 >> JavaScript >> 关于replace替换指定位置的单词解决思路
  详细解决方案

关于replace替换指定位置的单词解决思路

热度:376   发布时间:2012-02-08 19:52:21.0
关于replace替换指定位置的单词
有字符串
“LeftMessage is null and SentEmail is null and MetWith is null and SlsmanDist like 'KEE - kevin tang%”

先检索出有SlsmanDist这个单词,
如果有,将其前面的and改为OR,
其它的and不变,字符串长度是可变的

------解决方案--------------------
JScript code
var test = "LeftMessage is null and SentEmail is null and MetWith is null and SlsmanDist like 'KEE - kevin tang";

    alert(test.replace(/and SlsmanDist/g, function() {
            return "or SlsmanDist";
        }));

------解决方案--------------------
JScript code
var str="LeftMessage is null and SentEmail is null and MetWith is null and SlsmanDist like 'KEE - kevin tang%";
alert(str.replace(/and\s+(SlsmanDist)/gi,"Or $1"))

------解决方案--------------------
JScript code
var str = "LeftMessage is null and SentEmail is null and MetWith is null and  SlsmanDist";
alert(str.replace(/\band\b(?=\s*\bSlsmanDist\b)/img,"OR"));

------解决方案--------------------
看了上面几位,我不敢发言了...

------解决方案--------------------
探讨

JScript code
var str="LeftMessage is null and SentEmail is null and MetWith is null and SlsmanDist like 'KEE - kevin tang%";
alert(str.replace(/and\s+(SlsmanDist)/gi,"Or $1"))
  相关解决方案