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, '')
Related
This question already has an answer here:
How can I exclude some characters from a class?
(1 answer)
Closed 3 years ago.
I want to split a paragraph of text using regex, but /\W+/ splits can't into can and t.
Is there any way to define a regular expression that splits whenever a character other than a-z,A-Z,0-9 and '(single quotes) is encountered?
text=text.split(/\W+/);
The following regex splits on everything you asked for , that is a-z, A-Z, 0-9, and ' :
text=text.split(/[^a-zA-Z0-9\']/);
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
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:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 7 years ago.
My input String is like
abc,def,wer,str
Currently its splitting only on comma but in future it will contain both comma and newline.
Current code as below:
$scope.memArray = $scope.memberList.split(",");
In future I need to split on both comma and newline what should be the regex to split both on comma and newline.
I tried - /,\n\ but its not working.
You can use a regex:
var splitted = "a\nb,c,d,e\nf".split(/[\n,]/);
document.write(JSON.stringify(splitted));
Explanation: [...] defines a "character class", which means any character from those in the brackets.
p.s. splitted is grammatically incorrect. Who cares if it's descriptive though?
You could replace all the newlines with a comma before splitting.
$scope.memberList.replace(/\n/g, ",").split(",")
Try
.split(/[\n,]+/)
this regex should work.
This question already has answers here:
Regex to find not start and end with dot and allow some special character only not all
(3 answers)
Closed 2 years ago.
I am using following
ng-pattern="/^[a-zA-Z][a-zA-Z0-9._](.*[a-zA-Z0-9])?$/"
The matching String should
not start with a special character,
not end with special character, and
not include consecutive symbols except . (dot) and _ (underscore).
But it is not working.
Please, any suggestion.
Try using the word character class as a start ([\w] = [a-zA-Z0-9_]):
I'm not sure what you mean by consecutive symbols. But this might help:
/^[a-zA-Z]([\w.]*[a-zA-Z0-9])?$/
Maybe, have a look at the JavaScript RegExp Reference