Regular expression to validate the pattern - javascript

The pattern I need is below:
MA should be the first two characters of a string
The third character should be hyphen (-)
Characters 4 to 10 can be any numeric numbers (0-9)
The eleventh character should be hyphen (-)
Characters 12 to 15 can be any numeric numbers (0-9)
Example:
MA-1234567-1234
I have tried this:
/^(MA*)[0-9]{7}([0-9]{4})$/

The Regex you were using is missing the dashes between character sets, try this:
/^MA-\d{7}-\d{4}$/
Note that to incorporate this with an input box you would need to test against this Regex on keyup, something like this:
var re = /^MA-\d{7}-\d{4}$/;
$('#code').keyup(function() {
if (re.test(this.value)) {
console.log('The code is valid...');
}
});
Example fiddle

Nearly there you were missing dashes for separators and you don't need the paranthesis if you don't want to extract any of the information.
Correct regex is:
/^MA-[0-9]{7}-[0-9]{4}$/
Edit to satisfy new requirements
To match both: CW291291 and MA-1234567-1234
Use the pipe symbol '|' with both patterns to match either one:
/^CW[\d]{6}|MA-[\d]{7}-[\d]{4}$/

Related

A regex to allow exactly 5 digit number with one optional white space before and after the number?

With the regex in the jQuery code below, I can restrict the input text field to accept only 5 digits but it allows more white spaces before and after the number than just one. I mean to match a 5-digit number that is at the beginning of the string or is preceded by at most one space and is at the end of the string or is followed by at most one space. Please help me refine my regex to meet my requirement.
jQuery("input:text[name='address_1[zip]']").change(function(e) {
if (!jQuery(this).val().match(/\b\d{5}\b/g)) {
alert("Please enter a valid zip code");
jQuery("input[name='address_1[zip]']").val("");
return false;
}
});
I might suggest just using lookarounds here:
/(?<![ ]{2})\b\d{5}\b(?![ ]{2})/
This pattern says to:
(?<![ ]{2}) assert that 2 (or more) spaces do NOT precede the ZIP code
\b\d{5}\b match a 5 digit ZIP code
(?![ ]{2}) assert that 2 (or more) spaces do not follow
Here is a demo showing that the pattern works.
I think this will do.
/^\s?\d{5}\s?$/
Here's the pure JavaScript test code. You will have to convert this into JQuery style.
let p = /^\s?\d{5}\s?$/
let input = ' 54525 ';
let match = p.test( input );
console.log( match )
You need to parse the beginning of line and end of line
match(\A\s?\d{5}\s?\z/g)
This mean:
\A begin of string
\s? One optional space
\d{5} five digits
\s? One optional space
\z end of string

How to write a regex which matches anything but a number

I am trying to create an angular directive to be used with HTML inputs to filter out none numeric characters
Here is my regex I am using to achieve that:
inputValue.replace(/[^0-9\-\.]/g, "").replace(/\.(\.)/g, '$1');
However this regex does not cover these cases:
--5
5.5.6
-5-5
If I'm not wrong, this is really simple.^^
\d
\d machtes every digit from [0-9]. You can test our RegEx very simple at https://regex101.com without writing any javascript code to test it.
Edit:
You might want to add a * to the \d.
\d*
The * is the greedy selector, which selects all of the type before.
In your regex you use a negated character class [^0-9\-\.] which matches not a digit 0-9, a - and a . so you are keeping those matches.
If you want to match anything but a number you could use [^0-9] or \D to match any character that is not a digit and replace that with an empty value.
let inputValue = `--5
5.5.6
-5-5
!##$%# $%#% $%435 452545`;
inputValue = inputValue.replace(/\D/g, "");
console.log(inputValue);

Regex for international numbers - no space

I am trying to create a regEx that will have an optional '+' as the first character and then after accept all digits, making sure no spaces are allowed.
I have tried the following, but it does not seem to work :
new RegExp(/^\+?\d $/);
Seems like you want something like this,
new RegExp(/^\+?\d+$/);
Replace space in your regex with +. \d+ matches one or more digits.

Validating phone number using regex with support for multuple numbers

I am not very experienced with regex and I need to validate phone numbers using javascript.
I have a textbox which need to be allowed to accept multiple phone numbers with a delimiter of ';' and the characters that can be allowed for the phone numbers are
Numbers
'+'
'-'
Could someone help me on how I can acheive this using javascript and regex/ regular expressions?
Example:
+91-9743574891;+1-570-456-2233;+66-12324576
I tried the following:
^[0-9-+;]+$
Am not sure if this is correct.
You have placed - in wrong place so, your regex is not working.
Try this(your RegEx, but slightly modified):
^[0-9+;-]+$
or
^[-0-9+;]+$
To include a hyphen within a character class then you must do one of the following:
escape the hyphen and use \-,
place hyphen either at the beginning or at the end of the character class.
As the hyphen is used for specifying a range of characters. So, regex engine understands [0-9-+;]+ match any of the characters between 0 to 9, 9 to +(all characters having decimal code-point 57[char 9] to 43[char +] and it fails) and ;.
To be a bit more restrictive, you could use the following regexp:
/^\+[0-9]+(-[0-9]+)+(;\+[0-9]+(-[0-9]+)+)*$/
What it will match:
+91-9743574891
+1-570-456-2233;+66-12324576
What it won't match:
91-9743574891
+15704562233
6612324576
How about this ^([0-9\-\+]{5,15};?)+$
Explanation:
^ #Match the start of the line
[0-9\-\+] #Allow any digit or a +/- (escaped)
{5,15} #Length restriction of between 5 and 15 (change as needed)
;? #An optional semicolon
+ #Pattern can be repeat once or more
$ #Until the end of the line
Only as restrictive as specified could be tighter, See it working here.
Your regex will match what you allow, but I would be a bit more restrictive:
^\+?[0-9-]+(?:;\+?[0-9-]+)*$
See it here on Regexr
That means match an optional "+" followed by a series of digits and dashes. Then there can be any amount of additional numbers starting with a semicolon, then the same pattern than for the first number.

regex sets of 5 digits followed by a comma (trailing comma optional)

I've got the following regex:
(\d{5}[,])
This matches the following:
12005,11111,11111,
but how do I make the trailing comma optional?
EDIT:
Acceptable results would be:
12005,
11111,11111,
12005
11111,11111
Unacceptable:
123456
123456,
12345,123456
123456,123456
(\d{5})(?:,|$)
should do the trick.
To break this down,
\d{5} - 5 digits
(?:...) - just using parentheses to surround the |
, - a literal comma
$ - end of input
,|$ - a comma or end of input.
The |$ part is needed to avoid spuriously matching groups of digits not separated by commas like "01234567889".
To see it in action, try
JSON.stringify(
["01234", "01234,", "01234,56789", "01234,56789", "", "0123456789"]
.filter(
function (s) {
return /^(?:(\d{5})(?:,|$))+$/.test(s);
}))
which uses a larger RegExp to match one or more of these groups, so emits
["01234","01234,","01234,56789","01234,56789"]
(\d{5}[,]?)
Will match
12005,11111,11111,
or
12005,11111,11111
Perhaps this:
((?:\d{5},)*\d{5})
Will work if one set of 5 numbers or more than one separated by commas. Or you could get fully explicit and slap the start and end on it:
^((?:\d{5},)*\d{5})$
to make sure you don't match 5 digits from numbers with 6 or more digits, use a word boundary assertion (\b) and beginning of line assertion (^), like so:
(?:\b|^)(\d{5})(?:,|$)
\d{5}[,]* - 0 or more or \d{5}[,]? - 0 or 1.

Categories

Resources