Javascript正则表达式-如何在

Javascript正则表达式-如何在,javascript,regex,string,Javascript,Regex,String,我正在使用这个正则表达式: var regex = /\<.*?.\>/g 正如所料,我得到: ["<is>", "<use>", "<regular>"] 但是没有反斜杠,很抱歉有任何潜在的混乱 我怎样才能得到相反的结果?i、 e.我需要什么样的正则表达式不返回包含在之间的项 我尝试了/^\/g和其他类似的组合,包括方括号之类的东西。我有很多很酷的结果,只是没有什么是我想要的 我要说的是:基本上我想搜索和替换子字符串,但我想排除一些搜索空间,

我正在使用这个正则表达式:

var regex = /\<.*?.\>/g
正如所料,我得到:

["<is>", "<use>", "<regular>"]
但是没有反斜杠,很抱歉有任何潜在的混乱

我怎样才能得到相反的结果?i、 e.我需要什么样的正则表达式不返回包含在之间的项

我尝试了/^\/g和其他类似的组合,包括方括号之类的东西。我有很多很酷的结果,只是没有什么是我想要的

我要说的是:基本上我想搜索和替换子字符串,但我想排除一些搜索空间,可能使用<和>。我真的不想要破坏性的方法,因为我不想拆开字符串,改变它们,并担心重建它们


当然,我可以通过搜索字符串“手动”完成这项工作,但我认为正则表达式应该能够很好地处理这一问题。唉,我的知识不在需要的地方

使用已经创建的变量,尝试使用replace。它也是非破坏性的

str.replace(regex, '');
--> "This  a string to  to test the  expression"

使用已经创建的变量,尝试使用replace。它也是非破坏性的

str.replace(regex, '');
--> "This  a string to  to test the  expression"

啊,好吧,对不起,我误解了你的问题。这是一个在javascript中使用纯正则表达式很难解决的问题,因为javascript不支持lookbehinds,通常我认为我会使用lookaheads和lookbehinds来解决这个问题。一种做作的方式是这样的:

str.replace(/((?:<[^>]+>)?)([^<]*)/g, function (m, sep, s) { return sep + s.replace('test', 'FOO'); })

// --> "This <is> a string to <use> to FOO the <regular> expression"
进入

更新

像这样的东西也会剥去角色:

str.replace(/((?:<[^>]+>)?)([^<]*)/g, function (m, sep, s) { return sep.replace(/[<>]/g, '') + s.replace(/test/g, 'FOO'); })

"This test <is> a string to <use> to test the test <regular> expression"
--> "This FOO is a string to use to FOO the FOO regular expression"

啊,好吧,对不起,我误解了你的问题。这是一个在javascript中使用纯正则表达式很难解决的问题,因为javascript不支持lookbehinds,通常我认为我会使用lookaheads和lookbehinds来解决这个问题。一种做作的方式是这样的:

str.replace(/((?:<[^>]+>)?)([^<]*)/g, function (m, sep, s) { return sep + s.replace('test', 'FOO'); })

// --> "This <is> a string to <use> to FOO the <regular> expression"
进入

更新

像这样的东西也会剥去角色:

str.replace(/((?:<[^>]+>)?)([^<]*)/g, function (m, sep, s) { return sep.replace(/[<>]/g, '') + s.replace(/test/g, 'FOO'); })

"This test <is> a string to <use> to test the test <regular> expression"
--> "This FOO is a string to use to FOO the FOO regular expression"
试试这个正则表达式:

\b\w+\b(?!>)
更新

要支持括号内的空格,请尝试使用此括号。它不是纯粹的regex.match,但它可以工作,而且上面的答案更简单:

alert('This <is> a string to <use use> to test the <regular> expression'.split(/\s*<.+?>\s*/).join(' '));
试试这个正则表达式:

\b\w+\b(?!>)
更新

要支持括号内的空格,请尝试使用此括号。它不是纯粹的regex.match,但它可以工作,而且上面的答案更简单:

alert('This <is> a string to <use use> to test the <regular> expression'.split(/\s*<.+?>\s*/).join(' '));

这里有一种方法可以自定义替换标记之外的所有内容,并从标记的零件中去除标记

一般来说,你可以使用

function replaceOutsideTags(str, replacer) {
    return str.replace( /[^<>]+|<.*?>/g, function(val){
        if(val.charAt(0) == '<' && val.charAt(val.length - 1) == '>') {
          // Just strip the < and > from the ends
          return val.slice(1,-1);
        } else {
          // Let the caller decide how to replace the parts that need replacing
          return replacer(val); 
        }
    })
}
// And call it like
console.log(
    replaceOutsideTags( str, function(val){
        return val.toUpperCase();
    })
);

这里有一种方法可以自定义替换标记之外的所有内容,并从标记的零件中去除标记

一般来说,你可以使用

function replaceOutsideTags(str, replacer) {
    return str.replace( /[^<>]+|<.*?>/g, function(val){
        if(val.charAt(0) == '<' && val.charAt(val.length - 1) == '>') {
          // Just strip the < and > from the ends
          return val.slice(1,-1);
        } else {
          // Let the caller decide how to replace the parts that need replacing
          return replacer(val); 
        }
    })
}
// And call it like
console.log(
    replaceOutsideTags( str, function(val){
        return val.toUpperCase();
    })
);

如果我理解正确的话,您希望对字符串应用一些自定义处理,除了受保护的部分?如果是这样,您可以这样做:

// The function that processes unprotected parts
function process(s) {
    // an example could be transforming whole part to uppercase:
    return s.toUpperCase();
}

// The function that splits string into chunks and applies processing
// to unprotected parts
function applyProcessing (s) {
    var a = s.split(/<|>/),
        out = '';

    for (var i=0; i<a.length; i++)
        out += i%2
                ? a[i]
                : process(a[i]);

    return out;
}

// now we just call the applyProcessing()
var str1 = 'This <is> a string to <use> to test the <regular> expression';
console.log(applyProcessing(str1));
// This outputs:
// "THIS is A STRING TO use TO TEST THE regular EXPRESSION"

// and another string:
var str2 = '<do not process this part!> The <rest> of the a <string>.';
console.log(applyProcessing(str2));
// This outputs:
// "do not process this part! THE rest OF THE A string."
基本上就是这样。它返回整个字符串,并处理未受保护的部分

请注意,如果角括号不平衡,拆分将无法正常工作


有很多地方可以改进,但我将把它作为练习留给读者;p

如果我理解正确,您希望对字符串应用一些自定义处理,但受保护的部分除外?如果是这样,您可以这样做:

// The function that processes unprotected parts
function process(s) {
    // an example could be transforming whole part to uppercase:
    return s.toUpperCase();
}

// The function that splits string into chunks and applies processing
// to unprotected parts
function applyProcessing (s) {
    var a = s.split(/<|>/),
        out = '';

    for (var i=0; i<a.length; i++)
        out += i%2
                ? a[i]
                : process(a[i]);

    return out;
}

// now we just call the applyProcessing()
var str1 = 'This <is> a string to <use> to test the <regular> expression';
console.log(applyProcessing(str1));
// This outputs:
// "THIS is A STRING TO use TO TEST THE regular EXPRESSION"

// and another string:
var str2 = '<do not process this part!> The <rest> of the a <string>.';
console.log(applyProcessing(str2));
// This outputs:
// "do not process this part! THE rest OF THE A string."
/\b[^<\W]\w*(?!>)\b/g
基本上就是这样。它返回整个字符串,并处理未受保护的部分

请注意,如果角括号不平衡,拆分将无法正常工作

有很多地方可以改进,但我将把它作为练习留给读者;p

/\b[^<\W]\w*(?!>)\b/g
这是可行的,测试一下:

var str = 'This <is> a string to <use> to test the <regular> expression.';
var regex = /\<.*?.>/g;
console.dir(str.match(regex));
var regex2 = /\b[^<\W]\w*(?!>)\b/g;
console.dir(str.match(regex2));
这是可行的,测试一下:

var str = 'This <is> a string to <use> to test the <regular> expression.';
var regex = /\<.*?.>/g;
console.dir(str.match(regex));
var regex2 = /\b[^<\W]\w*(?!>)\b/g;
console.dir(str.match(regex2));

这是将正则表达式参数传递给core String.split方法的完美应用程序:

var results=str.split//;
简单

这是将正则表达式参数传递给核心字符串的完美应用程序。split方法:

var results=str.split//;
简单

你是说它返回[,]?顺便说一句,没必要逃出Ta,哦,是的,doh!是的,这就是我的意思!无法获取格式设置right@MattStyles,我知道您已经选择了答案,但请查看我的解决方案。希望有帮助!Cheers@cbayram感谢您的输入,这看起来也是一个很好的解决方案。您的意思是它返回[,]?顺便说一句,没必要逃出Ta,哦,是的,doh!是的,这就是我的意思!无法获取格式设置right@MattStyles,我知道您已经选择了答案,但请查看我的解决方案。希望有帮助!Cheers@cbayram感谢您的输入,这看起来也是一个很好的解决方案。不过,在OP替换了他们需要的标签之外的东西之后,您如何将匹配的东西取回呢?OP想要保留它,汉克斯,我到了那个阶段,但我真正想做的是用字符串中的“TADA”替换“test”,但仍然包含这些内容。我可以搜索“测试”

当然,但它可能包含在<和>中,因此仍将被替换。基本上,我想在任何不在<>@mattcha的地方替换“test”。要解决您刚才描述的问题,请尝试str.replace/[^我一直在考虑以某种方式使用$1等。这种方法很好,但我用字符串'str=this a test That this this regex is right one'对它进行了测试,不幸的是,它更改了<>中的'test',所以我最后用了这个TADA,this regex is right one,你如何在操作后获得匹配的内容但是,将他们需要的东西放在标签之外?OP想保留它,我到了那个阶段,但我真正想做的是用字符串中的“TADA”替换“test”,但仍然包含这些内容。我当然可以搜索“test”,但它可能包含在<和>中,因此仍然会被替换。基本上,我想重播ce“测试”不在<>@MattStyles中的任何地方。要解决您刚才描述的问题,请尝试str.replace/[^我一直在考虑以某种方式使用$1等。这种方法很好用,但我用字符串“str=this a test That this regex is right one”对它进行了测试,不幸的是,它更改了<>中的“test”,所以我最终得到了这个TADA,this regex is right one。这与depot发布的内容有什么不同?如何“OP应该得到[,]回到字符串中。这得到一个-1,因为它重复了现有答案的问题,证明我错了,你得到了一个-1。好的,对不起,我应得的-1。我想我在阅读depot的答案之前没有理解这个问题。我已经更新了我的答案,以更好的方式:-啊,好的,它还需要去掉separator characters:-我想我的答案是按照OP的要求去做,你觉得呢?这和depot发布的有什么不同?OP应该如何获得[,]回到字符串中。这得到一个-1,因为它重复了现有答案的问题,证明我错了,你得到了一个-1。好的,对不起,我应得的-1。我想我在阅读depot的答案之前没有理解这个问题。我已经更新了我的答案,以更好的方式:-啊,好的,它还需要去掉separator characters:-我想我的答案是按照OP的要求做的,你认为呢?什么?请解释,我不明白,我想投反对票,我只是回来,这对我来说是有意义的。但我不能让它帮助我,我遗漏了什么?这只是一个前瞻性的断言。我们在<>中捕获所有内容,除了后面列出的字符串?!和sepa按管道|评级。寻找其他参考。抱歉,似乎我误解了这个问题。我以为您希望获得除少数预定义项之外的所有项。更改。这太棒了!但是,当我使用复杂的str(如此)进行测试时,这个regex是正确的测试,然后它会在中间的空间中挣扎<>。我需要在某个地方添加a\s吗?什么?请解释,我搞不懂,想投反对票,我只是回来,这对我来说很有意义。但我不能让它帮助我,我缺少什么?这只是一个前瞻性断言。我们捕获<>中的所有内容,除了后面列出的字符串?!和用管道|分隔的字符串。查找其他引用或y、 似乎我误解了这个问题。我以为您希望获得除少数预定义项之外的所有条目。更改。这太棒了!但是,当我使用复杂的str进行测试时,例如,a测试,这是一个正则表达式,这是正确的测试,然后它与中间之间的空间进行斗争。<>。我需要在某个地方添加a\s吗?这看起来太棒了!明天我会花一些适当的时间去理解它是如何工作的!我几乎完全肯定这会解决我正在研究的真正问题。谢谢你花时间!这看起来很好!我明天会花一些适当的时间去理解它是如何工作的!我几乎完全肯定这会解决我正在研究的真正问题。谢谢你的帮助慢慢来!我要做所有的运动!谢谢老兄!我要做所有的运动!谢谢老兄!