0

How do you replace every letter in a string with an underscore in javascript.

For example I would like the string of "name" to be replaced with _ _ _ _.

Is there a way I can specify letters a-z and replace it with an underscore.

Thanks in advance.

1 Answer 1

5

Simple,

str.replace(/[a-z]/g, '_')

to replace both upper and lowercase letters,

str.replace(/[a-z]/gi, '_')
  • [a-z] matches any lowercase letter.
  • g global modifier helps to do the match globally.
  • i case-insensitive modifier helps to do a case insensitive match.
Sign up to request clarification or add additional context in comments.

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.