|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Str_Replace(Str, Find, Replace, Flags)
Str_Replace = function(Str, Find, Replace, Flags) {
/*
Purpose
Used to replace a specified string with another string.
Parameters
Str (string) = The string were operating on.
Find (string) = The string we're searching for.
Replace (string) = The string we're replacing Find with.
Flags (optional string) = Determines how the replace is performed.
This is a character string that is sent to the RegExp(Flags) parameter.
Some valid valids:
"g" Perform a global replace. All instances of Find are replaced.
"i" Perform a case-insensitive replace.
Default if falsey: "g"
Return (string)
Returns a string with Find replaced with Replace.
Version History
1.0 2015.12.06 Tested by Russ Tanner.
*/
// Default parameters
Flags = Flags || 'g';
// Escape the Find string so any special characters can be used.
FindEscaped = Find.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
// Perform the replacement and return the result.
var re = new RegExp(FindEscaped, Flags);
return Str.replace(re, Replace);
}
Last edited by Russ; 12/06/15 08:44 PM.
|
|
|
|
|