0

I am trying to replace string with data.replace, its working fine if using hard code or static value. But now i want to replace multiple values with loop but its not working.

My Code:

for(var i = 0; i<words.length; i++){
    var r = words[i];
    data = data.replace(/\[(\[qid:{r})\]]/g, words[i]);
}

Words contains:

Array [ "hid_1", "hid_2", "hid_6", "hid_7" ]

and my data is:

Site: [[qid:hid_1]]<br>

Block: [[qid:hid_2]]<br>

Nimewo kay la: [[qid:hid_6]]<br>

Latitude: [[qid:hid_7]]

its an HTML content.

i just need variable here:

for(var i = 0; i<words.length; i++){

        var r = words[i];
        data = data.replace(/\[(\[qid:hid_1)\]]/g, 'test');
               //data.replace(/\[(\[qid:{r})\]]/g, 'test');

    }
9
  • What output do you expect? Commented Jul 20, 2016 at 14:26
  • Expecting: State: hid_1 Block: hid_2 Commented Jul 20, 2016 at 14:27
  • Looks like you are using the same regex on each iteration, so each one will be replaced with the first word - is that what's happening? Commented Jul 20, 2016 at 14:27
  • actually i have to replace with another one, only for testing i am just replacing with that array valus Commented Jul 20, 2016 at 14:28
  • i just want to use variable inside replace..but i can't Commented Jul 20, 2016 at 14:29

3 Answers 3

1
var words = [ "hid_1", "hid_2", "hid_6", "hid_7" ];

var data = "Site: [[qid:hid_1]]<br>\
Block: [[qid:hid_2]]<br>\
Nimewo kay la: [[qid:hid_6]]<br>\
Latitude: [[qid:hid_7]]";

for(var i = 0; i<words.length; i++){
        var r = words[i];
        var reg = new RegExp('\\[\\[qid:' + r +'\\]\\]');
        data = data.replace(reg, r);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could just remove the characters that don't belong. Then, you don't need the other array of replacement strings.

EDIT

If your data is one long string, you can do the following:

var data = 'Site: [[qid:hid_1]]<br> Block: [[qid:hid_2]]<br> Nimewo kay la: [[qid:hid_6]]<br> Latitude: [[qid:hid_7]]';

data = data.replace(/\[\[qid:(.*?)]](?:<br>)?/g, '$1');

console.log(data);


Otherwise, if your data is in an array, you could do this:

var strings = [
  'Site: [[qid:hid_1]]<br>',
  'Block: [[qid:hid_2]]<br>',
  'Nimewo kay la: [[qid:hid_6]]<br>',
  'Latitude: [[qid:hid_7]]'
];

strings = strings.map(function(string) {
  return string.replace(/\[.*?:([^\]]*).*/, '$1')
});

console.log(strings);

1 Comment

I respect your work.. but did not need this..i just want to replace the searched content with data.replace(/[([qid:hid_1)]]/g, 'test'); see: data.replace(/[([qid:hid_1)]]/g, 'test'); Want: data.replace(/[([qid:{r})]]/g, 'test'); here "r" is a variable containing hid_1, hid_2 etc and i just want to replace with test..means in data.replace i just want to place variable {r} so that i can change the string in loop...thanks
0

is this what you are doing? Is seems fine.

 data =["Site: [[qid:hid_1]]",
"Block: [[qid:hid_2]]<br>",
"Nimewo kay la: [[qid:hid_6]]",
"Latitude: [[qid:hid_7]]" ];

var words =  [ "hid_1", "hid_2", "hid_6", "hid_7" ];
for(var i = 0; i<words.length; i++){

    var r = data[i];
    r = r.replace(/\[(\[qid:\w+)\]]/g, words[i]);
  console.log(r);

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.