how to ignore white space in regex [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
Using the code [A-Z]+[.] will select All-Caps words ending with a period. I was wondering how I would make this include an all-caps word behind it as well.
bold means it is the selected text
current: ASD ASD.
goal: ASD ASD.

If you want to match either one or two words but no more you can make the second word optional with the ? modifier:
(?:[A-Z]+\s+)?[A-Z]+\.
Or if you want to match as many words as there are before a period, you can use the * modifier instead:
(?:[A-Z]+\s+)*[A-Z]+\.
To obtain the index of the start of a match in JavsScript, you can do the following:
var regex = /(?:[A-Z]+\s+)?[A-Z]+\./;
var match = regex.exec('ASD ASD.');
console.log('The index of the first match is ' + match.index)

/[A-Z ]+[.]/
Will do what you describe.
Demo
You can do [A-Z\s]+[.] but that matches \n too.
Demo 2

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

No white space at beginning + allow space in the middle [duplicate]

This question already has answers here:
Regular expression: match start or whitespace
(8 answers)
Closed 1 year ago.
I have this regex for detecting #xxx
/(?:#)(.*[a-zA-Z0-9]*)/
it matches even when the #xxx is not separated from another string from the left (when it's typed in the middle of an input line).
xxx#xxx will match too so i added \s to require a space in the begining .Now it's
/\s(?:#)(.*[a-zA-Z0-9]*)/
But the problem is there isn't a match when the #xxx is typed in the begining of a line (the white space is still required) and i need it match in that case.
I tried to get inspired by https://stackoverflow.com/a/19973707/170592 so i added ^[^-\s] in the begining of the regex to make it
/^[^-\s](?:#)(.*[a-zA-Z0-9]*)/
But it didn't work neither.
I think that what you are looking for it is /\S+/ which means that check for any non-whitespace and I don't think you need the ^ at the beginning.
[-\S+](?:#)(.*[a-zA-Z0-9]*)

Remove everthing between two characters [duplicate]

This question already has answers here:
Strip HTML from Text JavaScript
(44 answers)
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 2 years ago.
How would you remove everything between all instances of brackets like in
var item = '<p>1. Get this <a title= "Static Review"> </a> more text </p>'
I've tried using the solution from How can I remove a character from a string using Javascript? with the global tag, formatted like : item = item.replace(/\/<.*>/, ''), but that just outputs nothing.
Really lost here
Just change your line of code by adding a ?:
item = item.replace(/<.*?>/g, '')
While .* is a greedy match, .*? is ungreedy. Greedy means "match as much as you can". Ungreedy means "match as few as possible". Thus <.*?> will stop at the first closing bracket, and do what you want.
The second change to your code: add the /g modifier. In Javascript, /g means "match all occurences", while without, the regex only matches the first occurrence.

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.

Regex pattern to match this string [duplicate]

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

Categories

Resources