regex to mask phone number such that except first 2 and last 2 characters in a number are replaced with * in javascript

javascript regex
mask account number javascript
javascript regex match
javascript replace
regex mask credit card number javascript
regex in node js
javascript regex test
javascript input mask phone number

Convert the given number 557878767 to 55*****67

The following code does precisely what you asked for however without using regex:

function convert(number){
    number = String(number)
    length = number.length-4
    middle = '*'.repeat(length)
    return number[0]+number[1]+middle+number[number.length-2]+number[number.length-1]
}

Regular Expressions :: Eloquent JavaScript, First, since a forward slash ends the pattern, we need to put a backslash Some characters, such as question marks and plus signs, have special Regular expression objects have a number of methods. Any character except for newline It is also possible to specify a range this way: {2,4} means the element must occur  This regex detects a specific number only if the number is grouped into small group of 3 digits separated by dash. Similarly taking another example, 12-2345-543, here the group size varies. Hence, to ensure that both the examples are detected, one need to modify the regex to detect 2 as well as 3 digits for the first group and 3 as well as 4 digits for the second group.

You can substitute (?<=\d\d)\d(?=\d\d) with *.

Demo: https://regex101.com/r/IcSN7M/1

Regular Expressions in JavaScript, Kevin Yank takes a look at what regular expressions are and what they can do for you. June 2, 2016 expressions are used in JavaScript, with a practical example or two to In our phone number example, we could write a simple regular Any character except a newline \n // A newline character \r // A  Regular Expression to such as 333-333-1234. Character classes. any character except newline \w \d \s: word, digit, whitespace

Welcome to stackoverflow

You can mask the phone number in single line (using regex).

var str = "557878767";
str = str.replace(/(?<=\d\d)\d(?=\d{2})/g, "*");
console.log(str);

Working with Phone Numbers in JavaScript, So based on that, we know that the regular expression apparoach isn't as simple as we first thought – but that's only the half of it. These examples  Arguments. The REGEXP_REPLACE () function takes 6 arguments: 1) source_string. is the string to be searched for. 2) search_pattern. is the regular expression pattern for which is used to search in the source string. 3) replacement_string. is the string that replaces the matched pattern in the source string.

Regular expressions, You construct a regular expression in one of two ways: A regular expression pattern is composed of simple characters, such The last example includes parentheses, which are used as a memory device. There is no match in the string "Grab crab" because while it contains Hide Newsletter Sign-up. A data-mask regex for scrubbing 16-digit VISA card numbers with optional spaces or dashes as matching delimiters between the number groups. Certain repetitive and sequential groups common to many false CCN s are ignored, impacting approximately 0.34% of potentially valid numbers.

RegExp, Starting with ECMAScript 6, new RegExp(/ab+c/, 'i') no longer throws a to match a name in the format first last and output it in the format last, first. str = '​John Smith' let newstr = str.replace(re, '$2, $1') console.log(newstr) Regular expression and Unicode characters lastIndex) // logs '15' // and so on. For a full reference to the particular regex flavors you'll be using, it's always best to go straight to the source. In fact, for some regex engines (such as Perl, PCRE, Java and .NET) you may want to check once a year, as their creators often introduce new features.

Regular expressions : Tricks you should know, A french phone number has two common formats : +33 6 68 56 23 05; 06 68 56 23 05. We will build a regex to match both ! ☎️  Hi, I have angular 5 with "angular2-text-mask": "^8.0.4" What I'm trying to do is unable the possibility to enter special characters, except for -, _, ', which should be allowed for a name type="text" input. I did the getting started ins

Comments
  • what have you tried already? read How to Ask and minimal reproducible example to improve your question. regards
  • Currently used this in react native, phoneRegEx = /(?!^.?).(?!.{0}$)/ but it gives exception
  • click on edit to improve it, not in comments. read the tour also
  • This uses lookbehind and thus requires ES2018 disqualifying IE and Edge
  • This uses lookbehind and thus requires ES2018 disqualifying IE and Edge.