19

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:

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

but this matches all occurrences, and what I need to do is somehow find and replace the nth occurrence.

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;
    }));
});
DJ Mosca
  • 240
  • 1
  • 2
  • 6
  • Im not entirely clear on what your trying to do ! using your example above can you show the expected output ... – Manse May 14 '12 at 14:00
  • Welcome to StackOverflow. I see that this question is not getting very many views, so I've edited your title to clearly reflect the question you're asking - how to find and replace the nth occurrence of an expression in brackets. – Vivian River May 14 '12 at 14:50

4 Answers4

46

Here is another possible solution. You can pass the string.replace function a function to determine what the replacement value should be. 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".

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

Vivian River
  • 31,198
  • 62
  • 198
  • 313
  • Thanks Daniel, this worked perfectly. I didn't know that the replace function had a callback function. – DJ Mosca May 14 '12 at 16:10
  • If this answered your question, then please click the check-mark next to this answer to mark it as the accepted answer. – Vivian River May 15 '12 at 02:58
  • 3
    This is awesome and helped me out quite a lot, but I needed to replace **every** nth character. To do that I replaced `(nth === 2)` with `(nth % 2 === 0)` – mikeybeck May 27 '15 at 01:16
2

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:

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) );
raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

If I understand the question correctly the RegExp shown below should do:

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/

strah
  • 6,702
  • 4
  • 33
  • 45
0

Short 'n simple, this is what I use for "Find nth & Replace":

function replNth(str,n,find,repl){var A=str.split(find);return A.slice(0,n).join(find)+repl+A.slice(n).join(find)}

Return str, replacing the nth occurrence of find with repl.

Example Usage:

var str='M1,2C3,4 5,6 7,8 9,10 11,12',
    newStr=replNth( str, 3, ' ', 'S' );
console.log( newStr ); / / "M1,2C3,4 5,6 7,8S9,10 11,12"
ashleedawg
  • 20,365
  • 9
  • 72
  • 105