regex- match exactly one occurrence of # - javascript

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

Related

Regex for not ending with specific words

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

Regex expression only matching the first occurrance

I'm trying to match all #mentions and #hashtags on a String using this RegEx expression:
(^|\s)([##][a-z\d-]+)
According to regex101.com, since the + is there it should match all occurances
"+" Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed
But when I run it through a String with more than one occurrance, it only matches the first.
What's going on?
Thanks for your attention.
Add the g (global) flag at the end for multiple matches.
/(^|\s)([##][a-z\d-]+)/g
^ this symbol defines beginning of string. That is why it only match with first string.
Use /[##]\w+/ regex.

RegEx matching help: won't match on each appearence

I need to write a little RegEx matcher which will match any occurrence of strings in the form of
[a-zA-Z]+(_[a-zA-Z0-9]+)?
If I use the regex above it does match the sections needed but would also match onto the abc part of 4_abc which is not intended. I tried to exclude it with:
(?:[^a-zA-Z0-9_]|^)([a-zA-Z]+(_[a-zA-Z0-9]+)?)(?:[^a-zA-Z0-9_]|$)
The problem is that the 'not' matches at the beginning and end are not really working like I hoped they would. If I use them on the example
a_d Dd_da 4_d d_4
they would block matching the second Dd_da because the space was used in the first match.Sadly I can't use lookarounds because I am using JS.
So the input:
a_d Dd_da 4_d d_4
should match: a_d, Dd_da and d_4
but matches: a_d (there is a space at the end)
Is there another way to match the needed sections, or to not consume the 'anchor' matches?
I really appreciate your help.
You can make use of \b:
\b[a-zA-Z]+(_[a-zA-Z0-9]+)?\b
\b matches the (zero-width) point where either the preceding character or following character is a letter, digit or underscore, but not both. It also matches with the start/end of the string if the first/last character is a letter, digit or underscore.

regex - how to select all double slashes except followed by colon

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,}/

Regex exclude doesn't exclude string only first character

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.

Categories