javascript regex to match a string ends with particular word [duplicate] - javascript

This question already has answers here:
Regular Expression only match if String ends with target
(3 answers)
Closed 5 months ago.
I have two strings
"track/bugfix merged to 'master'"
"track/bugfix merged to 'track/clonemaster'"
I want regex to match string ends with 'master' only
can anyone help me on this?

To get the end of a string, use the $ anchor, this signifies the end of a line.
So, the regular expression that you are looking for is as follows:
/'master'$/gi
const matches = [
"track/bugfix merged to 'master'",
"track/bugfix merged to 'track/clonemaster'"
].map(testString => !!testString.match(/'master'$/gi))
console.log(matches)

Related

how to find last occurrence of a parent in a given below string [duplicate]

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

Find String that starts and ends with a specific char [duplicate]

This question already has answers here:
Regex to match a string with specific start/end
(3 answers)
Closed 4 years ago.
I would like to find a string that starts and ends with a specific special character.
I tried the following regex but its not working:
(\#*\.|\&)[A-Za-z]+\.*#
I want to find any string that starts with #* and ends with *# but can't find the right regex for it.
Sample :
Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#
I am trying to find the string #*DCSN_RSN*# in the above string and replace it with <p>#*DCSN_RSN*#</p>
I think this is what you are looking for:
var sample = 'Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#';
var replaced = sample.replace(/(#\*[\s\S]*?\*#)/g, '<p>$1</p>');
console.log(replaced);
To match any characters you can use [\s\S]*, then add ? for [\s\S]*? to make it less-greedy. Your * characters also need to be escaped.

Getting grouped array from JavaScript REGEX [duplicate]

This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 4 years ago.
I'm currently generating a regex statement for a string comparing program I'm writing for my Discord bot. I am using the following generated regex string:
let regex = new RegExp("(TRIGGER WORD) (.*?)", "g");
I then execute it using the following:
let returns = regex.exec("TRIGGER WORD WHAT");
I get the following:
[ 'stud, ', 'stud,', '', index: 0, input: 'stud, what' ]
Which has no indication of grouping for 'WHAT' which I am trying to get most importantly.
From a background in PHP, I am expecting that I will get an array back such as the following:
['TRIGGER WORD', 'WHAT']
In your regex, the last part (.*?) is lazy and will repeat the dot as few times as possible.
For your example data you could match the quantifier in the second group greedy (.*). Your results are in capturing group 1 and 2.
This will match TRIGGER WORD in the first group, a whitespace and what is following in the second group.
let regex = new RegExp("(TRIGGER WORD) (.*)", "g");
let returns = regex.exec("TRIGGER WORD WHAT");
console.log(returns);
console.log(returns[1]);
console.log(returns[2]);

Javascript Regex Not Giving Multiple Results [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 5 years ago.
I am trying to read a string formatted like
<test>input</test>\n <another>input</another>
My regex works for the test tagged input, but ignores the another tagged input. If I wrap the entire regex in parenthesis and use the brackets {} to specify how many times, then it only saves the last match case. How can I catch and save all match cases?
My regex:
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/
Result contents of match:
<test>input</test>\n <another>input</another>
<another>input</another>
another
input
/input
Add a g Modifier so specify that it is global (allows for multiple results)
So change your regexp to (notice the g in the end)
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/g

Negative look-ahead always returns true [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 7 years ago.
I am using this regular expression
/(?!results)/i
As far as I understand, it will match any string that does not contain the word "result". However, when I try
/(?!results)/i.test('basketball results')
it returns true. How do I match strings that do not contain the word results?
This regex matches every position that has no results after it. See demo.
To match an expression that does not contain results, you need to use ^(?!.*results.*$).*. See another demo.
You can use a simple indexOf check here. It will return -1 is a substring is not contained in the string and zero or greater otherwise:
"basketball results".indexOf('results') == -1

Categories

Resources