Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace specific text characters across a document with JS

I'm wondering if there is a lightweight way I could use JavaScript or jQuery to sniff out a specific text character across a document; say and find all instances of this character. And then! Write an ability to replace all instances of this with say a $.

I found this snippet for starters:

var str = 'test: '';  str = str.replace(/'/g, "'"); 

Essentially; I am wanting a solution for a one page document. Grab all instances of X and make it XY. Only text characters.

like image 582
fred randall Avatar asked Sep 05 '13 18:09

fred randall


People also ask

How do you find and replace a character in a string in JavaScript?

Answer: Use the JavaScript replace() method You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.

How do you find and replace a word in a string in JavaScript?

To replace text in a JavaScript string the replace() function is used. The replace() function takes two arguments, the substring to be replaced and the new string that will take its place. Regex(p) can also be used to replace text in a string.

How do you replace all words with other words in HTML?

The JavaScript replace() method is used to replace any occurrence of a character in a string or the entire string. It searches for a string corresponding to either a particular value or regular expression and returns a new string with the modified values.


1 Answers

How about this, replacing @ with $:

$("body").children().each(function () {     $(this).html( $(this).html().replace(/@/g,"$") ); }); 

http://jsfiddle.net/maximua/jp96C/1/

like image 111
Max Malyk Avatar answered Sep 20 '22 15:09

Max Malyk