Javascript regex to replace numbers in sequence - Stack Overflow

I am trying to replace numbers in string with the character "X" which works pretty good by re

I am trying to replace numbers in string with the character "X" which works pretty good by replacing every individual number.

This the code:

 let htmlStr = initialString.replace(/[0-9]/g, "X");

So in a case scenario that initialString = "p3d8" the output would be "pXdX"

The aim is to replace a sequence of numbers with a single "X" and not every number (in the sequence) individually. For example:

If initialString = "p348" , with the above code, the output would be "pXXX". How can I make it "pX" - set an "X" for the whole numbers sequence.

Is that doable through regex?

Any help would be wele

I am trying to replace numbers in string with the character "X" which works pretty good by replacing every individual number.

This the code:

 let htmlStr = initialString.replace(/[0-9]/g, "X");

So in a case scenario that initialString = "p3d8" the output would be "pXdX"

The aim is to replace a sequence of numbers with a single "X" and not every number (in the sequence) individually. For example:

If initialString = "p348" , with the above code, the output would be "pXXX". How can I make it "pX" - set an "X" for the whole numbers sequence.

Is that doable through regex?

Any help would be wele

Share Improve this question edited Feb 6, 2019 at 6:08 Kamil Kiełczewski 92.9k34 gold badges395 silver badges370 bronze badges asked Feb 5, 2019 at 19:47 George GeorgeGeorge George 6013 gold badges8 silver badges18 bronze badges 3
  • 3 Add + after [0-9]. – Barmar Commented Feb 5, 2019 at 19:48
  • 1 You should read a regexp tutorial, this is one of the most basic patterns. – Barmar Commented Feb 5, 2019 at 19:48
  • If you did substantially edit your question or answers did not work for you, please ment and keep munity updated. – hc_dev Commented Feb 6, 2019 at 10:15
Add a ment  | 

3 Answers 3

Reset to default 8

Try

let htmlStr = "p348".replace(/[0-9]+/g, "X");
let htmlStr2 = "p348ad3344ddds".replace(/[0-9]+/g, "X");

let htmlStr3 = "p348abc64d".replace(/\d+/g, "X");

console.log("p348           =>",htmlStr);
console.log("p348ad3344ddds =>", htmlStr2);
console.log("p348abc64d     =>", htmlStr3);

In regexp the \d is equivalent to [0-9], the plus + means that we match at least one digit (so we match whole consecutive digits sequence). More info here or regexp mechanism movie here.

You can use + after [0-9] It will match any number(not 0) of number. Check Quantifiers. for more info

let initialString = "p345";
let htmlStr = initialString.replace(/[0-9]+/g, "X");
console.log(htmlStr);

Use the \d token to match any digits bined with + to match one or more. Ending the regex with g, the global modifier, will make the regex look for all matches and not stop on the first match.

Here is a good online tool to work with regex.

const maskNumbers = str => str.replace(/\d+/g, 'X');

console.log(maskNumbers('abc123def4gh56'));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744130590a4559824.html

相关推荐

  • Javascript regex to replace numbers in sequence - Stack Overflow

    I am trying to replace numbers in string with the character "X" which works pretty good by re

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信