I need to have a regex which matches the following -
/drama/uk
/drama/uk/
/drama/us
/drama/us/
/drama/fr
/drama/fr/
but should not match the following -
/drama/uk-uk
/drama/uk-uk/
/drama/us-us
/drama/us-us/
/drama/fr-fr
/drama/fr-fr/
So far I have managed to get the first par working like this
/\/drama\/\uk|uk\/|us|us\/|it|it\/|fr|fr\/$/
But I don't know how to get the second part working. Can any one help please
You could use an anchor to start the string ^, then match /drama/ and use an alternation with a non capturing group to list all the alternatives.
The slash at the end can be optional.
^\/drama\/(?:uk|us|fr)\/?$
Regex demo
Related
I want to match exactly one occurrence of # in a string. I found that /^[^#]+#[^#]+$/ working but not /[^#]+#[^#]+/
Why should I include the search from beginning to end? Wont the pattern anyway check throughout the string? Can someone explain it for me?
Without providing ^ and $, your RegEx will match parts of your string.
Let's demonstrate with some examples :
/^[^#]+#[^#]+$/
matches test#String.
doesn't match test#Str#ing
/[^#]+#[^#]+/
matches test#String
matches the part test#Str of test#Str#ing
I need some help with RegEx, it may be a basic stuff but I cannot find a correct way how to do it. Please help!
So, here's my question:
I have a list of URLs, that are invalid because of double slash, like this:
http://website.com//wp-content/folder/file.jpg, to fix it I need to remove all double slashes except the first one followed by colon (http://), so fixed URL is this: http://website.com/wp-content/folder/file.jpg.
I need to do it with RegExp.
Variant 1
url.replace(/\/\//g,'/'); // => http:/website.com/wp-content/folder/file.jpg
will replace all double slashed (//), including the first one, which is not correct.
example here:
https://regex101.com/r/NhCVMz/2
You may use
url = url.replace(/(https?:\/\/)|(\/){2,}/g, "$1$2")
See the regex demo
Note: a ^ anchor at the beginning of the pattern might be used if the strings are entire URLs.
This pattern will match and capture http:// or https:// and will restore it in the resulting string with the $1 backreference and all other cases of 2 or more / will be matched by (\/){2,} and only 1 occurrence will be put back into the resulting string since the capturing group does not include the quantifier.
Find (^|[^:])/{2,}
Replace $1/
delimited: /(^|[^:])\/{2,}/
Firstly we have the following string:
aaa{ignoreme}asdebla bla f{}asdfdsaignoreme}asd
We want our regex to find the whitespaces and any special charsacters like {}, but if after { comes exactly ignoreme} then exclude it
This is where we are right now:
(?!{ignoreme})[\s\[\]{}()<>\\'"|^`]
The problem is that our regex finds the } after ignoreme
Here is the link https://regex101.com/r/bU1oG0/2
Any help is appreciated,
Thanks
The point is that the } is matched since your (?!{ignoreme}) lookahead only skips a { followed with ignoreme} and matches a } since it is not starting a {ignoreme} char sequence. Also, in JS, you cannot use a lookbehind, like (?<!{ignoreme)}.
This is a kind of issue that can be handled with a regex that matches what you do not need, and matches and captures what you need:
/{ignoreme}|([\s[\]{}()<>\\'"|^`])/g
See the regex demo
Now, {ignoreme} is matched (and you do not have to use this value) and ([\s[]{}()<>\\'"|^`]) is captured into Group 1 the value of which you need to use.
Say I have an array of words, for example: (hi|ll|this|that|etc) and I want to find it in the following text:
Hi, I'll match this and ll too
I'm using: \\b(hi|ll|this|that|etc)\\b
But I want to only match whole words, excluding words found in contractions. Basically treat apostrophes as another "word seperator". In this case, it shouldn't match the "ll" in "I'll".
Ideas?
Use the apostrophe in addition to \b to begin and end a match:
(?:\b|')(hi|ll|this|that|etc)(?:\b|')
(?:...) means a non-capturing group. Stub on Regex101
If you want match just words you can try with:
(?:^|(?=[^']).\b)(hi|ll|th(?:is|at)|etc)\b
DEMO
and get words with group 1. However the \b will still allow to match fragments like: -this or #ll. I don't know is it desired result.
I'm trying to write a regular expression that will match a strings similar to the ones below:
Yu MSBE26
w AWAQBNL
I am using Javascript and have come up with the following regular expression:
(.*?(?:[AWMS\d]{2})[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
In words, I start my capture group off by matching everything until the [AWMS\d]{2} pattern is encountered, then I match the [AWMS\d]{2} pattern, the [A-Z]{2} that follows and finally the [\dA-Za-z]{1,3} to match the final two or three characters.
From what I have read, this should be working, but I'm not getting any matches.
For example when I use a regex tester I don't get any matches: Sample
Remove the second [AWMS\d]{2} - it looks like an accidental addition and is the reason your regex doesn't work:
(.*?(?:[AWMS\d]{2})[A-Z]{2}[\dA-Za-z]{1,3})
Edit: you don't even need the non capture group, the square brackets are enough:
(.*?[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
Your regex doesn't match your values because simply they don't match.
Your pattern is:
(.*?(?:[AWMS\d]{2})[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
Yu MSBE26
^--- fails here
w AWAQBNL
^--- fails here
Btw, you can use your regex to match your strings as this:
(.*?[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})
Working demo