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
Related
I want to create a RegExp in javascript. The requirement is to allow spaces at the beginning of a string if the string is non-empty. also, I have to restrict some special characters at the beginning of the string.
Currently, I am using this RegExp -
^(?!^[=+-#]).*
these strings need to be passed from regexp test -
foo
foo bar
bar
123bar
foo#bar.com
these string should fail -
#foo
#foo
' ...spaces'
'' empty-string
Please help.
You can use
^(?! *[=+#-]| +$).*
Or, to match any kind of whitespaces:
^(?!\s*[=+#-]|\s+$).*
Details:
^ - start of string
(?!\s*[=+#-]|\s+$) - a negative lookahead that fails the match if, immediately to the right from the current location, there is
\s*[=+#-] - zero or more whitespaces and then =, +, #, or -
| - or
\s+$ - one or more whitespaces till end of string
.* - zero or more chars other than line break chars as many as possible.
I have this ^[a-zA-Z0-9 #&$]*$, but not working for me in few cases.
If someone types
A string that only consists of digits (e.g. 1234567)
A string starting with a special character (e.g. &123abc)
need to be rejected. Note that a special char can be in the middle and at the end.
You seem to need to avoid matching strings that only consist of digits and make sure the strings start with an alphanumeric. I assume you also need to be able to match empty strings (the original regex matches empty strings).
That is why I suggest
^(?!\d+$)(?:[a-zA-Z0-9][a-zA-Z0-9 #&$]*)?$
See the regex demo
Details
^ - start of string
(?!\d+$) - the negative lookahead that fails the match if a string is numeric only
(?:[a-zA-Z0-9][a-zA-Z0-9 #&$]*)? - an optional sequence of:
[a-zA-Z0-9] - a digit or a letter
[a-zA-Z0-9 #&$]* - 0+ digits, letters, spaces, #, & or $ chars
$ - end of string.
you can do it with the following regex
^(?!\d+$)\w+\S+
check the demo here
I am writing a function to find attributes value from given string and given attribute name.
The input stings look like those below:
sip:+19999999999#trunkgroup2:5060;user=phone
<sip:+19999999999;tgrp=0180401;trunk-context=aaaa.aaaa.ca#10.10.10.100:8000;user=phone;transport=udp>
<sip:19999999999;tgrp=0306001;trunk-context=aaaa.aaaa.ca#10.10.10.100:8000;transport=udp>
<sip:+19999999999;tgrp=SMPPDIN;trunk-context=aaaa.aaaa.ca#10.10.10.100:8000;transport=udp>
After few hours I came out with this regular expression: /(\Wsip[:,+,=]+)(\w+)/g, but this is not working for the first example - as there is no not a word character before the attributes name.
How can I fix this expression to fetch both cases - <sip... and sip.. only when it is the beginning of the string.
I use this function to extract both sip and tgrp values.
Replace \W with \b, and use
\b(sip[:+=]+)(\w+)
Or, to match at the beginning of a string:
^\W?(sip[:+=]+)(\w+)
See the first regex demo and the second regex demo.
As \W is a consuming pattern matching any non-word char (a char other than a letter/digit/_) you won't have a match at the start of the string. A \b word boundary will match at the start of the string and in case there is a non-word char before s.
If you literally need to find a match at the beginning of a string after an optional non-word char, the \W must be replaced with ^\W? where ^ match the start of a string, and \W? matches 1 or 0 non-word chars.
Also, note that , inside a character class is matched as a literal ,. If you mean to use it to enumerate chars, you should remove it.
Pattern details:
\b - a word boundary
OR
^ - start of string
\W? - 1 or 0 (due to the ? quantifier) non-word chars (i.e. chars other than letters/digits and _)
(sip[:+=]+) - Group 1: sip substring followed with one or more :, + or = chars
(\w+) - Group 2: one or more word chars.
for begining of line use ^ and to make < is optional use ?
^<?(sip[:,+,=]+)(\w+)
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
Consider the following scenario (Javascript code):
regex = new RegExp((/([\d,.]+)[ $]/));
value = "2.879"
The regexp doesn't match value, but it matches (value+" ") therefore i think that the $ is not matched? Why is that?
Shouldn't the $ validate the end of string?
Special characters like $ don't have the same meaning inside a character class. In a character class they're just characters, so [ $] will match either the space character or the $ character. It won't match the end of a string.
If you want to match either a space character or the end of the string, you should use alternation, i.e. ( |$).