简体   繁体   English

在string中查找并替换第n次出现的[bracketed]表达式

[英]Find and replace nth occurrence of [bracketed] expression in string

I have a form where the name attributes get updated , but the problem is im using multidimensional values as follows: 我有一个表单,其中名称属性得到更新 ,但问题是使用多维值,如下所示:

<input type="text" name="questions[0][question]" />
<input type="text" name="questions[0][order]" />
<input type="text" name="questions[0][active]" />
<input type="text" name="answers[0][1][answer]" />
<input type="text" name="answers[0][2][answer]" />
<input type="text" name="answers[0][3][answer]" />

<input type="text" name="questions[1][question]" />
<input type="text" name="questions[1][order]" />
<input type="text" name="questions[1][active]" />
etc...

I need to change the value within the square brackets with JavaScript no matter what position they are in. I have tried using the following regular expression to match the value between the square brackets: 我需要使用JavaScript更改方括号内的值, 无论它们处于什么位置 。我尝试使用以下正则表达式来匹配方括号之间的值:

/(?<=\[)[^\]]*(?=\])/g

but this matches all occurrences, and what I need to do is somehow find and replace the nth occurrence. 但这匹配所有出现,我需要做的是以某种方式找到并替换第n次出现。

Or if there is another way to find and replace the values within the square brackets without using regular expressions I'm all ears. 或者,如果有另一种方法来查找和替换方括号内的值而不使用正则表达式我都是耳朵。

Thanks in advance 提前致谢

Resolved 解决

This final code is as follows: 最终的代码如下:

$('input', this).each(function(){
    var name = $(this).attr('name');
    var i = 0;
    $(this).attr('name', name.replace(/\[.+?\]/g,function (match, pos, original) {
    i++;
    return (i == 1) ? "[THE REPLACED VALUE]" : match;
    }));
});

Here is another possible solution. 这是另一种可能的解决方案 You can pass the string.replace function a function to determine what the replacement value should be. 您可以将string.replace函数传递给函数以确定替换值应该是什么。 The function will be passed three arguments. 该函数将传递三个参数。 The first argument is the matching text, the second argument is the position within the original string, and the third argument is the original string. 第一个参数是匹配文本,第二个参数是原始字符串中的位置,第三个参数是原始字符串。

The following example will replace the second "L" in "HELLO, WORLD" with "M". 以下示例将“HELLO,WORLD”中的第二个“L”替换为“M”。

var s = "HELLO, WORLD!";
var nth = 0;
s = s.replace(/L/g, function (match, i, original) {
    nth++;
    return (nth === 2) ? "M" : match;
});
alert(s); // "HELMO, WORLD!";

See MDN: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace 请参阅MDN: https//developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

The approach given in the accepted answer is concise and solid, but it has a drawback: if there's a big string with a lot of appearances of the given substring, it will be scanned till the end - even if one has to replace only at the beginning. 在接受的答案中给出的方法简洁而实用,但它有一个缺点:如果有一个大字符串,其中有很多外观的给定子字符串,它将被扫描到最后 - 即使只需要替换开始。 The alternative would be using 'exec', then breaking off the chain right after the replacement is done: 替代方案是使用'exec',然后在更换完成后立即断开链条:

 function replaceNthOccurence(source, pattern, replacement, n) { var substr = ''; while (substr = pattern.exec(source)) { if (--n === 0) { source = source.slice(0, substr.index) + replacement + source.slice(pattern.lastIndex); break; } } return source; } console.log( replaceNthOccurence('bla_111_bla_222_bla_333', /\\d+/g, '1st', 1) ); console.log( replaceNthOccurence('bla_111_bla_222_bla_333', /\\d+/g, '2nd', 2) ); console.log( replaceNthOccurence('bla_111_bla_222_bla_333', /\\d+/g, '3rd', 3) ); 

If I understand the question correctly the RegExp shown below should do: 如果我理解正确的问题,下面显示的RegExp应该:

var patt = /<input.*?(\[(\d)\])(\[(\d)\]){0,1}.*/g
var res = patt.exec(' <input type="text" name="questions[0][1][question]" />');

alert('First number: ' + res[2] + "\nSecond number: " + res[4]);

Demo here: http://jsfiddle.net/EUcdX/ 演示: http//jsfiddle.net/EUcdX/

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

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