Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all digits except the last four using regex.

I am using regex in javascript and want to replace all the digits in a number except the last four with '#'. I have figured out how to replace all the digits with '#', what do I do to not replace the last four? Here is my code so far. return cc.replace(/[0-9]/g, "#")

like image 731
Aaron Avatar asked Feb 19 '16 16:02

Aaron


People also ask

What is\\ in regex?

To match a literal space, you'll need to escape it: "\\ " . This is a useful way of describing complex regular expressions: phone <- regex(" \\(? #

Can regex replace characters?

RegEx can be effectively used to recreate patterns. So combining this with . replace means we can replace patterns and not just exact characters.

How to remove all characters except numbers from a string?

To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.

What is $1 in regex replace?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

use this pattern

\d(?=\d{4})

and replace with #
Demo

like image 86
alpha bravo Avatar answered Sep 19 '22 06:09

alpha bravo