3

This is kinda a follow-up question from this, in which a solution was provided for Notepad++ but unsuitable for JavaScript.

Let's say I have some random text:

let text = `aaaaaaaaaa
            5aaaa8aaaa
            4707aaaaaa
            a1aaaaaaaa
            923aaaaaaa`;

Now I want to replace each digit that appear after a newline with X, to achieve this end-result:

`aaaaaaaaaa
 Xaaaa8aaaa
 XXXXaaaaaa
 a1aaaaaaaa
 XXXaaaaaaa`

The solution provided for Notepad++ cannot be used here because the \G anchor is not available in JavaScript so text.replace(/(?:\G|^)\d/gm, 'X') does not work.

Are there any alternatives to using \G here, or any other ways to do this replacement in JavaScript?

3
  • try using \b\d+ Commented Aug 6, 2017 at 17:03
  • @baao that only changes the first number on each line, i.e. it changes the 3rd line from 4707aaaaaa to X707aaaaaa instead of XXXXaaaaaa as intended Commented Aug 6, 2017 at 18:46
  • Ah sorry - I've read it wrong then. @mozicid Commented Aug 6, 2017 at 20:15

1 Answer 1

1

One option is:

text.replace(/\b(\d+)/g, m => 'X'.repeat(m.length))
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.