This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group
Related
This question already has answers here:
Regex Last occurrence?
(7 answers)
Closed 3 months ago.
I have a below pattern string.
'GP0|#92ca7467-4c0d-461a-aac4-2bc8fd9ee16a;L0|#092ca7467-4c0d-461a-aac4-2bc8fd9ee16a|Analysts;GTSet|#7fa22453-62b1-4bec-b73d-01ccf115d558;GPP|#fd613617-ba9d-43e5-9990-95f96f94af2a;GPP|#ba395283-6169-4c6d-84d5-1cecb3c2a73b;GP0|#a547b95c-0dfa-4f42-b540-e55872fb2e81;L0|#0a547b95c-0dfa-4f42-b540-e55872fb2e81|Awards;GP0|#c4363ae8-8608-4309-92f0-5079c69b47e4;L0|#0c4363ae8-8608-4309-92f0-5079c69b47e4|Digital Workplace;GP0|#1976b988-a993-4f13-a1e8-d847138eebc6;L0|#01976b988-a993-4f13-a1e8-d847138eebc6|Intranet;GP0|#a68218df-b9e8-4f07-bfff-22cab83bbc0d;L0|#0a68218df-b9e8-4f07-bfff-22cab83bbc0d|Microsoft;GP0|#57737444-1a1b-4c87-a479-1548b58e44e3;L0|#057737444-1a1b-4c87-a479-1548b58e44e3|Research;'
I want to get last occurrence of the pattern which starts with GPP and ends with ;
expected output: GPP|#ba395283-6169-4c6d-84d5-1cecb3c2a73b;
I tried this regex /GPP\|.+?;/i but it gives the first occurrence i.e. GPP|#fd613617-ba9d-43e5-9990-95f96f94af2a;
As they come after each other, you can match the pattern and assert that there is no following occurrence starting with the same pattern:
As the match ends on a single character, you can match any character except ; instead using a negated character class.
\bGPP\|[^;]+;(?!GPP\|[^;]+;)
Regex demo
This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 7 months ago.
I need a reqular expression to not match this (/^[a-zA-Z][a-zA-Z0-9]+$/) pattern, where the string needs to start with alphabet followed by number and alphabet, with no special characters.
I tried with (/^?![a-zA-Z]?![a-zA-Z0-9]+$/) and not able to get appropriate answer.
Example:
P123454(Invalid)
PP1234(Invalid)
1245P(valid)
##$124(valid)
Thanks in advance.
^ means start with, So it should start with an alphabetic letter, then any number \d of alphabetic letters a-z with i case insensitive flag.
const check = (str) => {
return /^[^a-z].*/i.test(str)
}
console.log(check('P123454'))
console.log(check('PP1234'))
console.log(check('1245P'))
console.log(check('##$124'))
This regex might be helpful:
/^[^a-zA-Z]+.*$/g
Your every valid input (from the question) should be a match.
Regex 101 Demo
Explanation:
Does not allow string starting with a-zA-Z
Everything after than is allowed (I'm not sure if this is a requirement)
This question already has answers here:
Regex lazy vs greedy confusion
(2 answers)
Why does a simple .*? non-greedy regex greedily include additional characters before a match?
(3 answers)
Closed 3 years ago.
I'm trying this in javascript
/\/.*?$/.exec('foo/bar/tar')[0]
I was expecting to get /tar as result but getting /bar/tar. As far as I understand non-greed regex would take the smallest match.
I'm circumventing this with myvar.split('/').reverse()[0] but I couldn't understand what is going wrong with the regex.
There is nothing wrong with the regex but the pattern \/.*?$ matches from the first forward slash until the end of the string non greedy.
The dot matches any character except a newline and does not take a forward slash into account, so that will result in /bar/tar.
If you want to match /tar, you could match a forward slash, followed by not matching anymore forward slashes using a negated character class and then assert the end of the string.
\/[^\/]+$
Pattern demo
console.log(/\/[^\/]+$/.exec('foo/bar/tar')[0]);
This question already has answers here:
How can I match a pipe character followed by whitespace and another pipe?
(5 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I'm using this JS regex text.match(/|(https:.+?path.+?)|/)[1] to get a regex of a URL that is in between pipe | characters but it's not working.
The text is ||https://url.com/path/value|| but I can't seem to extract the URL from it. I need to have path in the middle to identify this particular URL since there are other URLs in the file.
It doesn't have to be a URL that I'm extracting. I mainly would like to know how to extract something from between a pair of characters (| in this case).
You need to escape the pipe ("|") characters:
text.match(/\|(https:.+?path.+?)\|/)[1]
Pipe is a special character that basically means "or". https://www.regular-expressions.info/alternation.html
To grab everything between the two sets of || then you could use this regex:
text.match(/\|\|(.*)\|\|/)
The first part \|\| matches the characters || literally.
The next part (.*)matches any character zero or more and groups the result.
The last part \|\| matches the closing characters || literally.
This question already has answers here:
Negative lookbehind equivalent in JavaScript
(11 answers)
Closed 5 years ago.
Lets say I have this string:
key something.key() (.key)(key)
I would like regex to only match the word "keys" that are inside "[ ]"
[key] something.key() (.key)([key])
I have used this regex currently /(?!\.)key/g
But that only excludes dot and still selects word even if it startet with a dot.
Use negative character class to match not a dot:
[^\.]
Then add ^ to match not a dot or at the beginning of the string:
([^\.]|^)
Add ?: to the group to make it non-capturing.
(?:[^\.]|^)
Finally add a capturing group matching your word:
(?:[^\.]|^)(word)
You could achieve the same result using negative look-behind:
(?<!\.)word
Alas, JavaScript regex doesn’t implement it.