Replacing JavaScript without overriding

Is there a non-regex replace() function in any of the existing JavaScript frameworks, or has it already been posted on the Internet somewhere as a one-time function?

For example, I want to replace "@!#$123=%" , and I do not want to worry about which characters should go. Most languages ​​seem to have both replacement methods. I would like this simple thing to be added.

+10
javascript
source share
4 answers

I may not understand your question, but javascript has replace()

 var string = '@!#$123=%'; var newstring = string.replace('@!#$123=%', 'hi'); 

edit : (see comments) The 5th edition seems to contain this information, although it does not appear when I'm directly . here is the relevant part:

The replace () method performs a search and replace operation. It takes a regular expression as its first argument and a replacement string as the second argument. It searches for the string on which it is called to match the specified pattern. If the g flag is set in the regular expression, the replace () method replaces all matches in the string with the replacement string; otherwise, it replaces only the first match found. If the first argument to replace () is a string, not a regular expression, the method searches for that string literally, and does not convert it to a regular expression using the RegExp () constructor, as search () does.

+23
source share

I had exactly the same problem finding the replace () method of a non-regex javascript string method. My solution was to use a combination of split () and join ():

 "some text containing regex interpreted characters: $1.00".split("$").join("£"); 

which gives:

"some text containing interpreted regular expression characters: £ 1.00"

compare with replace ():

 "some text containing regex interpreted characters: $1.00".replace(new RegExp("$"),"£") 

which bizarrely gives:

"some text containing interpreted regex characters: $ 1.00 £"

+14
source share

Try the following:

 function replaceAllTemp(str,find, replace) { var ignoreCase=true; var _token; var token=find; var newToken=replace; var i = -1; if ( typeof token === "string" ) { if ( ignoreCase ) { _token = token.toLowerCase(); while( ( i = str.toLowerCase().indexOf( token, i >= 0 ? i + newToken.length : 0 ) ) !== -1 ) { str = str.substring( 0, i ) + newToken + str.substring( i + token.length ); } } else { return this.split( token ).join( newToken ); } } return str; }; 
+3
source share

You can do this with or without case sensitivity.
Unfortunately, JavaScript indexOf does not accept the locale and invariant as an argument, so you have to replace toLowerCase with toLocaleLowerCase if you want to preserve the specificity of the culture.

 function replaceAll(str, find, newToken, ignoreCase) { var i = -1; if (!str) { // Instead of throwing, act as COALESCE if find == null/empty and str == null if ((str == null) && (find == null)) return newToken; return str; } if (!find) // sanity check return str; ignoreCase = ignoreCase || false; find = ignoreCase ? find.toLowerCase() : find; while (( i = (ignoreCase ? str.toLowerCase() : str).indexOf( find, i >= 0 ? i + newToken.length : 0 )) !== -1 ) { str = str.substring(0, i) + newToken + str.substring(i + find.length); } // Whend return str; } 

or as a prototype:

 if (!String.prototype.replaceAll ) { String.prototype.replaceAll = function (find, replace) { var str = this, i = -1; if (!str) { // Instead of throwing, act as COALESCE if find == null/empty and str == null if ((str == null) && (find == null)) return newToken; return str; } if (!find) // sanity check return str; ignoreCase = ignoreCase || false; find = ignoreCase ? find.toLowerCase() : find; while (( i = (ignoreCase ? str.toLowerCase() : str).indexOf( find, i >= 0 ? i + newToken.length : 0 )) !== -1 ) { str = str.substring(0, i) + newToken + str.substring(i + find.length); } // Whend return str; }; } 
0
source share

All Articles