javascript regex matching string conditions including special characters - javascript

I want to validate some strings.
Rules:
String can be contain A-Z, 0-9, "_", "!", ":".
If string contains 2x special characters, eg, "__" or "!!" or "K:KD:E" must return false.
Examples
Legitimate matches
FX:EURUSD
FX_IDC:XAGUSD
NYMEX_EOD:NG1!
Invalid matches:
0-BITSTAMP:BTCUSD - contains a minus sign)
2.5*AMEX:VXX+AMEX:SVXY - contains a *, a + and 2x ":"
AMEX:SPY/INDEX:VIX - contains a /

You can use this negative lookahead based regex:
/^(?:[A-Z0-9]|([_!:])(?!.*\1))+$/gm
RegEx Demo
([_!:])(?!.*\1) will ensure there is no repetition of special characters.

I would first start out with regex to remove all strings containing invalid characters:
/[^A-Z0-9_!:]/
I would then use this to check for duplicates:
/(_.*_)|(!.*!)|(:.*:)/
These can be combined to give:
/([^A-Z0-9_!:])|(_.*_)|(!.*!)|(:.*:)/
This can be seen in action here.
And here is a JSFiddle showing it working.

I eventrually used pattern
var pattern = /^[a-zA-Z_!0-9]+:?[a-zA-Z_!0-9]+$/;

Related

Split a string after two characters with regular expressions

I'm trying to split a string after both the "&" and the ":". The string is formatted like this:
Public Protection & Housing Authority: $100,000,000.
In Javascript, I'm using .split(/(?=[:&]+)/g), which breaks the string before the & and :. How can I get it to break after each of those characters?
You can use lookbehind so it breaks after the match.
Regex:
/(?<=[:&])/g
Check here for the example I made: https://regex101.com/r/vi565o/1
+\w+ should do the trick.
It means to include letters after the is string is matched.
It should be - .split(/((&)+\w+)/g)
You can actually match strings up to and including these two chars if present, or up to end of string:
const s = 'Public Protection & Housing Authority: $100,000,000.';
console.log(s.match(/[^&:]*[&:]?/g).filter(Boolean));
The g flag makes String#match return all non-overapping matches found in the string. The regex matches
[^&:]* - zero or more chars other than & and :
[&:]? - an optional ampersand or colon char.

what is javascript regex to validate an array of string value

I want to validate an array of a single element of type string using regex. The array should not be empty and should not start with any special symbol except # and the string can include only numbers and alphabets.
what i tried
[a-zA-z0-9s!##$%^&*()_/\+=.,~`]+
you can try
regex = /^#?[a-zA-Z0-9]+/gm;
Try this:
regex = /^#?[a-z0-9]+/gi;
Breakdown:
/^ - Match the start of the string
#? - Match an optional #
[a-z0-9] - Character set including the lowercase alphabet, and all ten digits
+ - Match one or more of the preceding character set
/gi - Global and case-insensitive flags

Write a regex for usernames

I want a Regex for my mongoose schema to test if a username contains only letters, numbers and underscore, dash or dot. What I got so far is
/[a-zA-Z0-9-_.]/
but somehow it lets pass everything.
Your regex is set to match a string if it contains ANY of the contained characters, but it doesn't make sure that the string is composed entirely of those characters.
For example, /[a-zA-Z0-9-_.]/.test("a&") returns true, because the string contains the letter a, regardless of the fact that it also includes &.
To make sure all characters are one of your desired characters, use a regex that matches the beginning of the string ^, then your desired characters followed by a quantifier + (a plus means one or more of the previous set, a * would mean zero or more), then end of string $. So:
const reg = /^[a-zA-Z0-9-_.]+$/
console.log(reg.test("")) // false
console.log(reg.test("I-am_valid.")) // true
console.log(reg.test("I-am_not&")) // false
Try like this with start(^) and end($),
^[a-zA-Z0-9-_.]+$
See demo : https://regex101.com/r/6v0nNT/3
/^([a-zA-Z0-9]|[-_\.])*$/
This regex should work.
^ matches at the beginning of the string. $ matches at the end of the string. This means it checks for the entire string.
The * allows it to match any number of characters or sequences of characters. This is required to match the entire password.
Now the parentheses are required for this as there is a | (or) used here. The first stretch was something you already included, and it is for capital/lowercase letters, and numbers. The second area of brackets are used for the other characters. The . must be escaped with a backslash, as it is a reserved character in regex, used for denoting that something can be any character.

Regex validate string if it does not contain more than special count of special characters

I'm developing a pattern that validates string if it does not contain more then two matches of #. here is code:
^[^\!|\#|\$|\%|\^|\&|\*|\+][\w]*([\w ]+\#[\w ]*){0,2}$
[^!|\#|\$|\%|\^|\&|*|+]
this is group of not acceptable symbols.
additionally, the pattern should validate string in case if it contains other symbols( - _ , . / ). each symbol should have it's own counter and should not match in any position more than two times.
for example if i have s string like this:
Mcloud dr. #33/#45, some text, some text
it should be valid. but in this case should not:
Mcloud dr. ###33/$#45, ####, ----
What would you suggest ?
Given that you want to match alphanumerics characters and some special symbols ()-_,./ You have to mention them in a character class like this.
Regex: ^(?!.*([(),.#/-])\1)([\w (),.#/-]+)$
Explanation:
(?!.*([(),.#/-])\1) asserts that there shouldn't be more than one character mentioned in character class. This asserts from beginning of string to end.
([\w (),.#/-]+) matches the rest of the string for allowed characters from beginning to end.
Regex101 Demo

Comma separated numbers regex - accepts pipe character

I have written a small regex for javascript. It should only accept numbers separated by commas.
Valid examples are:
1 single value allowed
1,278,3780,50
1,56,90, (trailing comma allowed)
Invalid examples are:
1,45 67
1, gj, + (any special character and characters)
The regex is: /^[\d|\,]+/g
However, it also accepts | (pipe character).
Like: 1|46|6778|567
What am I doing wrong? What did I miss?
Please follow this link to my regex
You don't need pipe (|) and escaping characters within character class. Also as a proper way you can use following regex:
/^(?:\d+\,)+\d+$/g
Debuggex Demo
As i missed your edit if the trailing comma is a valid case you can simply use following regex :
^(\d+,?)+$
Try this -
^\d+\,(?:\d+\,?)+$
Demo
EDIT:
With changed requirements -
^\d+(?:,\d+)*,?$
Demo here
To match a number separated by comma:
(\d+,?)+
The correct regex is as follows:
^\d+(?:,\d+)*,?$
This will match the cases specified:
A single number (1)
A series of numbers delimited by commas (1,2,3)
An optional trailing comma (1,, 1,2,3,4,)

Categories