10 examples of 'javascript string replace regex' in JavaScript

Every line of 'javascript string replace regex' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
92export function replace(str, regexp){
93
94 for(let i = 0, len = regexp.length; i < len; i += 2){
95
96 str = str.replace(regexp[i], regexp[i + 1]);
97
98 if(!str){
99
100 break;
101 }
102 }
103
104 return str;
105}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

339export function replace(str, search, replace) {
340 return str.replace(new RegExp(escape(search), "g"), replace);
341}
60function jsReplacer(match)
61{
62 if (count++ >= n)
63 {
64 return match;
65 }
66 var i = arguments.length - 3;
67 var submatches = new Array(i);
68 while (i > 0)
69 {
70 var submatch = arguments[i];
71 submatches[--i] = submatch === undefined
72 ? _elm_lang$core$Maybe$Nothing
73 : _elm_lang$core$Maybe$Just(submatch);
74 }
75 return replacer({
76 match: match,
77 submatches: _elm_lang$core$Native_List.fromArray(submatches),
78 index: arguments[i - 1],
79 number: count
80 });
81}
122export function escapeRegex(value: string): string {
123 let final = value;
124 for (let char of REGEX_SPECIAL_CHARACTERS) {
125 final = final.replace(char, `\\${char}`);
126 }
127 return final;
128}
41static escapeRegex(str) {
42 return String(str)
43 .replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1')
44 .replace(/\x08/g, '\\x08');
45}
23function escapeRegex(s) {
24 return typeof s === 'string' ? s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') : s;
25}
20function replaceAll (input, find, replace) {
21 let regex = new RegExp(find, 'g');
22 return input.toString().replace(regex, replace);
23};
103function replaceAll(string, find, replace){
104 return string.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
105}
1038function replace(regex, options) {
1039 regex = regex.source;
1040 options = options || '';
1041 return function self(name, val) {
1042 if (!name) {
1043 return new RegExp(regex, options);
1044 }
1045 val = val.source || val;
1046 // val = val.replace(/(^|[^\[])\^/g, '$1'); // "~+^]|" => "~+]|"
1047 regex = regex.replace(name, val);
1048 return self;
1049 };
1050}
118function replace(string, matches, replacement) {
119 matches = Array.isArray(matches) ? matches : [matches];
120
121 matches.some(match => {
122 if (string.match(match)) {
123 string = string.replace(match, replacement);
124 return true;
125 }
126 });
127
128 return string;
129}

Related snippets