Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find and replace tokens in javascript

I have to do something like this

string  = " this is a good example to show"

search  = array {this,good,show}

find and replace them with a token like

string  = " {1} is a {2} example to {3}" (order is intact)

the string will undergo some processing and then

string  = " {1} is a {2} numbers to {3}" (order is intact)

tokens are again replaced back to the string likem so that the string becomes

string  = " this is a good number to show"

how to make sure that the pattern is matched and the same tokens are replaced

for example /[gG]ood/ is a pattern to search and replaced later with appropriate "case".Or in other words if ^\s*[0-9]+. is the pattern the matched string needs to be stored and replace to form the original text as it was

How should it be implemented so that the process is done at high performance ?

Thanks in advance.

like image 228
Sourabh Avatar asked May 06 '10 09:05

Sourabh


People also ask

What is replace () in JavaScript?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.

What is $1 in replace JavaScript?

In your specific example, the $1 will be the group (^| ) which is "position of the start of string (zero-width), or a single space character". So by replacing the whole expression with that, you're basically removing the variable theClass and potentially a space after it.

How to replace two string in js?

replace(/cat/gi, "dog"); // now str = "I have a dog, a dog, and a goat." str = str. replace(/dog/gi, "goat"); // now str = "I have a goat, a goat, and a goat." str = str. replace(/goat/gi, "cat"); // now str = "I have a cat, a cat, and a cat."

How to replace a string in JavaScript from variable?

In JavaScript, replace() is a string method that is used to replace occurrences of a specified string or regular expression with a replacement string. Because the replace() method is a method of the String object, it must be invoked through a particular instance of the String class.


2 Answers

You don't mention anything about multiple occurrences of the same token in the string, I guess you'll be replacing all occurrences.

It would go something like this:

var string = "This is a good example to show, this example to show is good";
var tokens = ['this','good','example'];

for (var i = 0; i < tokens.length; i++) {
    string.replace(new RegExp(tokens[i], "g"),"{"+i+"}");
}
// string processing here
for (var i = 0; i < tokens.length; i++) {
    string.replace(new RegExp("{"+i+"}","g"),tokens[i]);
}
like image 122
Dan Burzo Avatar answered Nov 15 '22 11:11

Dan Burzo


var string = "this is a good example to show"
var search = ["this","good","show"] // this is how you define a literal array

for (var i = 0, len = search.length; i < len; i++) {
   string.replace(RegExp(search[i], "g"), "{" + (i+1) + "}")
}

//... do stuff

string.replace(/\{(\d+)\}/, function(match, number) {
  if (+number > 0)
    return search[+number - 1];
});
like image 40
RoToRa Avatar answered Nov 15 '22 13:11

RoToRa