0

I want convert this string:

string with characters like áéíóú

to

#################################

This "parágrafo".replace(/\wW*/g, "#") returns "###á#####"

6
  • @downvotes why? Commented Jun 16, 2018 at 14:23
  • So you want to convert all letters of string to #? Commented Jun 16, 2018 at 14:23
  • Yes @Eddie thats it Commented Jun 16, 2018 at 14:24
  • What's the purpose of W* in the regexp? Commented Jun 16, 2018 at 14:36
  • 2
    why not just "parágrafo".replace(/./g,'#') ?? Commented Jun 16, 2018 at 14:37

5 Answers 5

2

One option is using repeat()

let str = 'string with caracthers like áéíóú';

let result = "#".repeat(str.length);

console.log(result);

Doc: repeat()

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

1 Comment

To bad repeat isn't supported in Internet Explorer at the moment.
1

The other answers using .repeat() are probably better ways to do this. This answer mainly serves to explain what's wrong with your code and how it could be done correctly.

\w doesn't match accented letters for some reason, that's why á ends up in the output. But even if this weren't a problem, it wouldn't match the spaces between words.

You can use ., which matches any character except newline.

console.log("string with caracthers like áéíóú`".replace(/./g, "#"));

If you also want to replace newlines with #, add the s modifier.

console.log(`string with caracthers like áéíóú
and also newlines`.replace(/./gs, "#"));

Oops, that's a very new feature in ES2018, not available in all browsers yet. You can use /.|\n.g for better portability.

console.log(`string with caracthers like áéíóú
and also newlines`.replace(/.|\n/g, "#"));

6 Comments

In which browsers would that s modifier work atm? Firefox seems to fail on it.
Apparently that's a nonstandard extension. The standard JS flags are only gimuy
That's an extension of ECMA2018. It means it is part of the standard but not implemented yet by all major browsers.
So does that mean that Chrome already supports the look-behinds? I've read somewhere that it would be in the new ECMA. (don't have Chrome installed on this pc to test it)
@LukStorms Yes! "abcde".match(/(?<=b)c/) returns "c"
|
0
"#".repeat("string with characters".length)

No need for replace.

Comments

0

repeat() cannot be used in IE so I will suggest looping over to the str length to get string with value #.

let str = 'string with caracthers like áéíóú';
let result ='';
for(var i=0; i<str.length; i++){
  result += '#';
}
console.log(result);

2 Comments

@Barmar i didn't get you.
You still say to split the string and loop. You're not splitting any more.
0

It looks like your regex lost a few characters somewhere (and gained a *). It should be /[\w\W]/ instead:

var str = "parágrafo".replace(/[\w\W]/g, "#");

console.log(str);

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.