Regex in angularjs - javascript

I have to create a regex to validate ticket string in correct format.
Ticket format:
SFHD00002523003
It starts with four alphabets and ends with 12 numeric characters.
This is my code in my angular controller:
var pattern = new RegExp('[A-Z]{4}\\d{12}$');
console.log(pattern.test('SFHD00002523003');
Unfortunately, it is returning false for correct string, too.

Need double backslash instead of single. like: \\d instead of \d in your expression
var pattern= new RegExp('([A-Z]){4}\\d{12}');
and your string has 11 numeric chars not 12

You forgot the start of string anchor ^ to make sure you only match 16 character string. If you do not use it, you will get a match inside a 17+ character string that ends with your pattern.
Thus, use
var pattern = /^[A-Z]{4}\d{12}$/;
^

Related

Regex to accept special character only in presence of alphabets or numeric value in JavaScript?

I have a Javascript regex like this:
/^[a-zA-Z0-9 !##$%^&*()-_-~.+,/\" ]+$/
which allows following conditions:
only alphabets allowed
only numeric allowed
combination of alphabets and numeric allowed
combination of alphabets, numeric and special characters are allowed
I want to modify above regex to cover two more cases as below:
only special characters are not allowed
string should not start with special characters
so basicaly my requirement is:
string = 'abc' -> Correct
string = '123' -> Correct
string = 'abc123' ->Correct
string = 'abc123!##' ->Correct
string = 'abc!##123' -> Correct
string = '123!##abc' -> Correct
string = '!##' -> Wrong
string = '!##abc' -> Wrong
string = '!##123' -> Wrong
string = '!##abc123' -> Wrong
can someone please help me with this?
You can require at least one alphanumeric:
/^(?=[^a-zA-Z0-9]*[a-zA-Z0-9])[a-zA-Z0-9 !##$%^&*()_~.+,/\" -]+$/
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Also, I think you wanted to match a literal -, so need to repeat it, just escape, change -_- to \-_, or - better - move to the end of the character class.
The (?=[^a-zA-Z0-9]*[a-zA-Z0-9]) pattern is a positive character class that requires an ASCII letter of digit after any zero or more chars other than ASCII letters or digits, immediately to the right of the current location, here, from the start of string.
Just add [a-zA-Z0-9] to the beginning of the regex:
/^[a-zA-Z0-9][a-zA-Z0-9 \^\\\-!##$%&*()_~.+,/'"]+$/gm
Note, if within a class (ie [...]) that there are four special characters that must be escaped by prefixing a backward slash (\) to it so that it is interpreted as it's literal meaning:
// If within a class (ie [...])
^ \ - ]
// If not within a class
\ ^ $ . * + ? ( ) [ ] { } |
RegEx101
If all the special characters are allowed and you consider that underscore _ is also a special character then you can always simplify your RegEx like this :
/^[^\W_].+$/
Check here for your given examples on Regex101

regex custom lenght but no whitespace allowed [duplicate]

I have a username field in my form. I want to not allow spaces anywhere in the string. I have used this regex:
var regexp = /^\S/;
This works for me if there are spaces between the characters. That is if username is ABC DEF. It doesn't work if a space is in the beginning, e.g. <space><space>ABC. What should the regex be?
While you have specified the start anchor and the first letter, you have not done anything for the rest of the string. You seem to want repetition of that character class until the end of the string:
var regexp = /^\S*$/; // a string consisting only of non-whitespaces
Use + plus sign (Match one or more of the previous items),
var regexp = /^\S+$/
If you're using some plugin which takes string and use construct Regex to create Regex Object i:e new RegExp()
Than Below string will work
'^\\S*$'
It's same regex #Bergi mentioned just the string version for new RegExp constructor
This will help to find the spaces in the beginning, middle and ending:
var regexp = /\s/g
This one will only match the input field or string if there are no spaces. If there are any spaces, it will not match at all.
/^([A-z0-9!##$%^&*().,<>{}[\]<>?_=+\-|;:\'\"\/])*[^\s]\1*$/
Matches from the beginning of the line to the end. Accepts alphanumeric characters, numbers, and most special characters.
If you want just alphanumeric characters then change what is in the [] like so:
/^([A-z])*[^\s]\1*$/

Why does this regular expression evaluate to false in javascript?

I'm looking for a string that is 0-9 digits, no other characters.
This is alerting me with a "false" value:
var regex = new RegExp('^[\d]{0,9}$');
alert(regex.test('123456789'));
These return true, and I understand why (The ^ and $ indicate that the whole string needs to match, not just a match within the string) :
var regex = new RegExp('[\d]{0,9}');
alert(regex.test('123456789'));
-
var regex = new RegExp('[\d]{0,9}');
alert(regex.test('12345678934341243124'));
and this returns true:
var regex = new RegExp('^[\d]{0,9}');
alert(regex.test('123456789'));
So why, when I add the "$" at the end would this possibly be failing?
And what do I need to do to fix it?
When you use
var regex = new RegExp('^[\d]{0,9}$');
syntax, you'll get regex as
/^[d]{0,9}$/
Note the \d is turned into d.
This regex /^[d]{0,9}$/ will match only the d zero to nine times.
RegExp uses string to define regex, the \ is also used as escape symbol in the string, so \ in \d is considered as the escape character.
Escape the \ by preceding it with another \.
var regex = new RegExp('^[\\d]{0,9}$');
I'll recommend you to use regex literal syntax rather than the confusing RegExp syntax.
var regex = /^\d{0,9}$/;
EDIT:
The reason you get true when using var regex = new RegExp('^[\d]{0,9}'); is because the regex implies that the string should start with any number of d include zero to nine. So, event when the string does not starts with d the condition is stratified because of 0 as the minimum no of occurrences.
You might want to check if the string starts with one to nine digits.
var regex = /^\d{1,9}$/;
You should use the regular expression literal (without quotes and using the beginning and ending slashes) when defining the RegExp object. This is the recommended approach when the regular expression will remain constant, meaning it does not need to be compiled every time it is used. This gives you the desired result:
var regex = new RegExp(/^[\d]{0,9}$/);
Because $ means End of line, and your string does not have an end of line as last character
May be you are looking for "\z"

Regular expression to get the string including the starting character

I have a string like this
var str="|Text|Facebook|Twitter|";
I am trying to get any one of the word with its preceding pipe sign so something like
|Text or |Facebook or |Twitter
I thought of below two patterns but they didn't work
/|Facebook/g //returned nothing
/^|Facebook/g // returned "Facebook" but I want "|Facebook"
What should I use to get |Facebook?
The pipe is special character in a regular expression. A|B matches A or B.
You have to escape the pipe to match | literally.
var str = '|Text|Facebook|Twitter|'
str.match(/\|\w+/g) // => ["|Text", "|Facebook", "|Twitter"]
\w matches any alphabet, digit, _.
You should escape | char:
/\|Facebook/g

Regular expression to match a number range in JavaScript

I'm having problem with JavaScript regular expressions. I want to match real numbers form 1 to 5. Precission is in two digits. My code is but it doesnt work.
function validate_prosjek(nmb)
{
var pattern= new RegExp(/[1-4]\.[0-9][0-9]|5\.00/);
return pattern.test(nmb);
}
It recognizes real numbers higher than 5.
You need to "anchor" your regexp with ^ and $ to match the beginning and end of the string, respectively:
var pattern = /^([1-4]\.[0-9][0-9]|5\.00)$/;
You also need to escape the . because it's a special character in regexps, and there's no need to call new RegExp if the regexp is already in /.../ synax.

Categories

Resources