Remove everthing between two characters [duplicate] - javascript

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.

Related

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]*)

Removing all occurrences of '^' from a String [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
I have this sorta string = "^My Name Is Robert.^"
I want to remove the occurrences of ^ from this string. I tried the replace method like :
replyText.replace(/^/g, '');
But it hasn't any affect. Using the replace without the global works but only removes the first occurrence.
Should I just make a loop and keep looping the string with replace till no more '^' are contained, or is there a better way?
You need to escape the ^ character in RegEx:
replyText.replace(/\^/g, '');
The caret, ^, is a special character in Regex, therefore it has to be escaped with a backslash i.e
replyText.replace(/\^/g, '')

how to ignore white space in regex [duplicate]

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

How to use end of string in square brackets in javascript regex? [duplicate]

This question already has answers here:
Using $ anchor inside a character class does not work
(2 answers)
Closed 3 years ago.
In js regex, I have
[\.\?!][\s$]
what I want to do is match
literal dot, or literal question mark or explanation mark
then
either 1 whitespace character or, be at the end of the string.
However the regex above, tries to match the literal $.
Does anyone know how to fix this?
Thanks
Try this Regex:
[.?!](?:\s|$)
Click for Demo
Explanation:
[.?!] - matches either a . or a ? or a ! literally
(?:\s|$) - matches either a white-space or the End-of-line

JS regex get text between characters [duplicate]

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.

Categories

Resources