0

In javascript if I have something like

string.replace(new RegExp(regex, "ig"), " ")

this replaces all found regexes with a single space. But how would I do it if I wanted to replace all found regexes with spaces that matched in length?

so if regex was \d+, and the string was

"123hello4567"

it changes to

"   hello    "

Thanks

4
  • 6
    something like .replace(new RegExp(regex, "ig"), m => ' '.repeat(m.length)) Commented Dec 6, 2017 at 1:18
  • wow that actually worked Commented Dec 6, 2017 at 1:21
  • 3
    I'm surprised too !! Commented Dec 6, 2017 at 1:21
  • @JaromandaX - You should post that as an answer (with explanation) so that it can be accepted before too many more wrong answers appear. Commented Dec 6, 2017 at 1:54

3 Answers 3

4

The replacement argument (2nd) to .replace can be a function - this function is called in turn with every matching part as the first argument

knowing the length of the matching part, you can return the same number of spaces as the replacement value

In the code below I use . as a replacement value to easily illustrate the code

Note: this uses String#repeat, which is not available in IE11 (but then, neither are arrow functions) but you can always use a polyfill and a transpiler

let regex = "\\d+";
console.log("123hello4567".replace(new RegExp(regex, "ig"), m => '.'.repeat(m.length)));

Internet Exploder friendly version

var regex = "\\d+";
console.log("123hello4567".replace(new RegExp(regex, "ig"), function (m) {
    return Array(m.length+1).join('.');
}));

thanks to @nnnnnn for the shorter IE friendly version

Sign up to request clarification or add additional context in comments.

2 Comments

Array(m.length+1).join(".") is an IE-friendly alternative to .repeat() if you want a one-liner.
been too long since I needed ES5 :p
0
"123hello4567".replace(new RegExp(/[\d]/, "ig"), " ")
1 => " "
2 => " "
3 => " "

"   hello    "

"123hello4567".replace(new RegExp(/[\d]+/, "ig"), " ")
123 => " "
4567 => " "
" hello "

1 Comment

it should for for any general regex.
0

If you just want to replace every digit with a space, keep it simple:

var str = "123hello4567";
var res = str.replace(/\d/g,' ');

"   hello    "

This answers your example, but not exactly your question. What if the regex could match on different numbers of spaces depending on the string, or it isn't as simple as /d more than once? You could do something like this:

var str = "123hello456789goodbye12and456hello12345678again123";
var regex = /(\d+)/;
var match = regex.exec(str);
while (match != null) {
    // Create string of spaces of same length
    var replaceSpaces = match[0].replace(/./g,' ');  
    str = str.replace(regex, replaceSpaces);
    match = regex.exec(str);
}

"   hello      goodbye  and   hello        again   "

Which will loop through executing the regex (instead of using /g for global).

Performance wise this could likely be sped up by creating a new string of spaces with the length the same length as match[0]. This would remove the regex replace within the loop. If performance isn't a high priority, this should work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.