Regular Expression - Avoid a string in expression - javascript

I am trying to create a regex that should match following cases.
if exact match of words 'first, second, third' then match should fail - but if there are any characters around it, then the string should be matched.
Also I need to avoid certain set of characters in the string. [()!=<>", ] - if these characters are part of string then match result should fail.
I looked at few examples & negative look ahead but did not get the right regex yet.
^(?!first$|second$|third$|fou rth$)[^()!=<>", ]+
desired output:
first - fail
second - fail
1first - pass
first1 - pass
1first1 - pass
fou rth - fail - it has space in between word and is from ignore list
newTest - pass
new(test - fail - since ( is not allowed character
space word - fail - since space is non allowed character
The regex needs to support case insensitive words
Any help appreciated. I am using javascript.

Try this Regex:
^(?!.*[()!=<>", ])(?!(?:first|second|third)$).+$
Click for Demo
Explanation:
^ - asserts the start of the string
(?!.*[()!=<>", ]) - negative lookahead to validate that the test string does not contain any of these characters - (, ), !, =, <, >, ,,
(?!(?:first|second|third)$) - At this moment we are at the beginning of the test string. This position should not be immediately followed by (first or second or third) and then by the end of the string($)
.+ - matches 1+ occurrences of any character but not a newline character
$ - asserts the end of the string

Related

regex to allow space and restrict special characters at the beginning of a non empty string

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.

Regex to validate hypen(-) at start and end of the string

I'm validating a string("-test-") whether it contains hypens(-) at start and end of the string using regex. So i found an regex to restrict hypen at start and end of regex.
/^(?!-)[a-zA-Z0-9-' ]+[^-]$/i
This regex was validating as expected when the string contains more than one char("aa") with or without hypen. But its not working as expected when i'm simply passing one character string("a") without hypen.
And also these need to allow special characters and alphanumeric characters like "$abcd&". Need to restirct oly hypen at start and end of the string.
Could you guys help out of this..
The pattern you have matches a string that consists of at least 2 chars because [a-zA-Z0-9-' ]+ needs 1 char to match and [^-] requires another char to be present.
You may revamp the lookahead to also fail a string that ends with -:
/^(?!-)(?!.*-$).+$/
^^^^^^^^
See the regex demo
Details
^ - start of a string
(?!-)(?!.*-$) - negative lookaheads that fail the match if the string starts with - or ends with -
.+ - any 1 or more chars other than line break chars (use [\s\S] to match any char)
$ - end of string.
An unrolled version for this pattern would be
^[^-]+(?:-+[^-]+)*$
See this regex demo
Details
^ - start of string
[^-]+ - 1 or more chars other than -
(?:-+[^-]+)* - 0+ sequences of
-+ - 1+ hyphens
[^-]+ - 1 or more chars other than -
$ - end of string.
To allow any character but only disallow hyphen at start and end:
^(?!-).*[^-]$
^ start of string
(?!-) look ahead if there is no hyphen
.* match any amount of any character
[^-] match one character, that is not a hyphen
$ at the end
See demo at regex101

Regex to match char after string

I'm attempting to match the first 3 letters that could be a-z followed by a specific character.
For testing I'm using a regex online tester.
I thought this should work (without success):
^[a-z]{0,3}$[z]
My test string is abcz.
Hope you can tell me what I'm doing wrong.
If you need to match a whole string abcz, use
/^[a-z]{0,3}z$/
^^
or - if the 3 letters are compulsory:
/^[a-z]{3}z$/
See the regex demo.
The $[z] in your pattern attempts to match a z after the end of string anchor, which makes the regex fail always.
Details:
^ - string start
[a-z]{0,3} - 0 to 3 lowercase ASCII letters (to require 3 letters, remove 0,)
z - a z
$ - end of string anchor.
You've got the end of line identifier too early
/^[a-z]{0,3}[z]$/m
You can see a working version here
You can do away with the [] around z. Square brackets are used to define a range or list of characters to match - as you're matching only one they're not needed here.
/^[a-z]{0,3}z$/m

regular expression to find word after a character and before another one if included

I have a url like:
image/media-group/rugby-league-programme-covers-3436?sort=title
or
image/media-group/rugby-league-programme-covers-3436
I need to get everything after media-group and not including ? or anything after.
So in both instances rugby-league-programme-covers-3436 is what I need to return
I used the regular expression /media-group/(.*)\? which works for the instance where there is a query string but not in the instance where there is no query string.
I am using the below code
var patt=new RegExp('/media-group/(.*)\?');
return patt.exec(url)[1];
Your help on this would be most appreciated
I believe the best pattern would be:
/^[^\#\?]+\/media-group\/([^\?]+).*$/
which breaks out as:
^ - start of string
[^\#\?]+ - one or more non-hash, non-question-marks
\/ - literal char
media-group - literal chars
\/ - literal char
( - start capture group
[^\?]+ - one or more chars non-question-marks
) - end of capture group
.* - zero or more chars
$ - end of string
The reason this works is because [^\?]+ is "greedy" in that it will attempt the longest possible match, which encompasses either a question-mark followed by arbitrary chars, or nothing, since all chars to the end of the string have already been captured in the non-question-mark capture group.
So, using
var RE=new RegExp(/^[^\#\?]+\/media-group\/([^\?]+).*$/),
url="image/media-group/rugby-league-programme-covers-3436?sort=title";
console.log(url.match(RE)[1])
prints: rugby-league-programme-covers-3436 and changing url to image/media-group/rugby-league-programme-covers-3436, produces the same result.
Update
Modified the pattern re David Foerster's comment.

JQuery Regular expression to accept alphanumeric characters and ' , -

I'm trying to figure out how to get my regular expression to accept certain special characters: ', , and - along with alphanumeric characters. I've had a stab at it but to no avail, and I'm quite new to regex, can anyone help?
Here was my attempt which, surprisingly, didn't work...
/^\d+/,\'\-\$/i
Something like this?
/[0-9a-zA-Z',-]+/
if it has to be a full string, you can use
/^[0-9a-zA-Z',-]+$/
Try
/^[\w',-]*$/
(assuming you mean ASCII letters, digits and underscore by "alphanumeric").
\d is shorthand for [0-9], which is not any alphanumeric character.
/^[\w,'-]+$/i
should do the trick.
What this is saying:
^ - match the start of the line
[ - match any of the following characters (group #1)
\w - any word (meaning differs depending on locale;
generally, any letter, number or the `-` character.)
, - a comma
' - an apostrophe
- - a dash
] - end group #1
+ - one or more times
$ - match the end of the line
/i - set case-insensitivity.

Categories