Regex mask last 4 numbers

  Kiến thức lập trình
R.replace(/[0-9](?!([0-9]{4}))/g,'*','123456789'); 
yields 12345****

want to input 123-45-6789 and yield 123-45-****

Currently based on above it yields ***-**-**** 

No idea why. I am using rambda js to simulate.
http://ramdajs.com/docs/#replace

need help to construct the regex for that . Any help is appreciated

3

Your current regex matches any digit that doesn’t have four other digits immediately following it. Which is the case for every digit in the string 123-45-6789.

If the last four characters of the strings you are working with are always digits you could easily do this without a regex.

But if you want a simple regex, you could search with the following regex and replace with ****.

d{4}$

Note that this regex wont match anything if the string doesn’t end with four digits. So it would match the first three of the examples below and fail the last three.

12-345-6789
123-45-6789
123456789
1-2-3-4-5-6-7-8-9
12-34-56-78-9
123-456-789

If you want a regex that will work in all six cases you could use this:

d(?=(?:D*d){0,3} *$)

0

R.replace(/d(?=(?:D*d){0,3} $)/g,’‘,’123-45-6789’); worked perfectly. Thanks heaps. Kudos to Francis !

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT