This question already has answers here:
Trim string in JavaScript
(20 answers)
Closed 4 years ago.
From this regex,
text.replace(/^\s+|\s+$/g,"").replace(/ +/g,' ')
how do I remove the regex just for trailing white space?
I am new to regex and did some research but I'm not able to understand the pattern.
/^\s+|\s+$/g means
^ // match the beginning of the string
\s+ // match one or more whitespace characters
| // OR if the previous expression does not match (i.e. alternation)
\s+ // match one or more whitespace characters
$ // match the end of the string
The g modifier indicates to repeat the matching until no match is found anymore.
So if you want to remove the part the matches whitespace characters at the end of the string, remove the |\s+$ part (and the g flag since ^\s+ can only match at one position anyway - at the beginning of the string).
Useful resources to learn regular expressions:
http://www.regular-expressions.info/
Regex in JavaScript (since this seems to be JavaScript).
Related
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:
Why this javascript regex doesn't work?
(1 answer)
Match exact string
(3 answers)
Closed 5 years ago.
Regex is the bane of my existence. I've done plenty tutorials, but the rules never stick, and when I look them up they seem to conflict. Anyways enough of my whining. Could someone tell me why this regex doesn't exclude hyphens or brackets:
/^[A-Za-z_][A-Za-z\d_]*/
The way I understand it (or at least what I'm trying to do), the ^ character dictates that the regex should start with the next thing on the list That means the regex should start with [A-Za-z_] or any character a-z and A-Z as well as and underscore _. Then the string can have anything that includes [A-Za-z\d_] which is any alphanumeric character and an underscore. Then I use the * to say that the string can have any number of what was presented previously (any alphanumeric character plus underscore). At no point to I specify a bracket [ or a hyphen -. Why does this expression not exclude these characters
Extra info
I'm verifying this with javascript:
function variableName(name) {
const reg = RegExp("^[A-Za-z_][A-Za-z\d_]*")
return reg.test(name)
}
function variableName("va[riable0") // returns true should be false
It's actually matching the first 2 letters("va"), that's why it's true.
To match the whole phrase, your reg expression should have "$" at the end:
"^[A-Za-z_][A-Za-z\d_]*$"
Your regex matches the part of the string that does not contain the bracket, because your're missing the $ anchor that would (together with ^) force it to match the whole string. Use
const reg = /^[A-Za-z_][A-Za-z\d_]*$/g
// ^
function variableName(name) {
return reg.test(name)
}
console.log(variableName("va[riable0"))
This question already has answers here:
Javascript split by spaces but not those in quotes
(3 answers)
Closed 2 years ago.
I am trying to split a string on white spaces only (\s), but that are not between a "quoted" section.
I am matching all text in between these quoted sections in the following manner:
(['"`]).*?\1
Regex101
However, when I try to add this as a negative lookahead, to only split on white spaces outside of those quotes, I can't get it to work:
\s(?!(['"`]).*?\1)
Regex101
How can I only split on the white spaces that are not in "quotes"?
\s(?=(?:[^'"`]*(['"`])[^'"`]*\1)*[^'"`]*$)
You can use this regex with lookahead to split upon.See demo.
https://regex101.com/r/5I209k/4
or if mixed tick types.
https://regex101.com/r/5I209k/7
The problem is that you need to exclude entries within the group. Instead of using a negative lookahead you could do it like this:
(\S*(?:(['"`]).*?\2)\S*)\s?|\s
Basically what it does is to:
captures any non-whitespace characters
that may contain a quoted string
and is optionally directly followed by any non-whitespace (e.g a comma after the quote).
then matches an optional trailing whitespace
OR
matches a single whitespace
Capture group1 will then contain an as long as possible sequences of all non-whitespace characters (unless they are within quotes). This can thus be used with the replacement group \1\n to replace your desired whitespaces with a newline.
Regex101: https://regex101.com/r/A4HswJ/1
JSFiddle: http://jsfiddle.net/u1kjudmg/1/
I'd use a simpler approach, no need of advanced features:
'([^']|\\.)*'|"([^"]|\\.)*"|`([^`]||\.)*`|\S*
meaning:
a single-quoted section '([^']|\\.)*'
or | a double-quoted section "([^"]|\\.)*"
or | a back-quoted section (can't place it inline in SO markdown)
or | an un-quoted section \S*
This will separate also quoted parts. If this is not wanted you can instead use
('([^']|\\.)*'|"([^"]|\\.)*"|`([^`]||\.)*`|\S)+
i.e. find sequences of tokens where each token is either a non-whitespace or a quoted section.
This question already has an answer here:
Match and replace whole words in javascript
(1 answer)
Closed 8 years ago.
I have a regex that matches these strings in a string; however, it is matching non-words ( parts-of-words ) as well.
For example city is matched as it contains it. However, I want only the string it to be matched it if it the only characters between whitespace. So it or he would match, but not city or where.
Here is the regex ( pretty basic and simple ): they|he|she|her|him|them|it.
How can I get it to match these words if the word is only this?
Use word boundaries to denote the beginning and ending of a word.
http://www.regular-expressions.info/wordboundaries.html
So your regex would become something on the order of:
\b(they|he|she|her|him|them|it)\b
Check it out
It should be noted that this regular expression won't match words containing apostrophes, e.g. can't, won't, etc. For a discussion of this, see the following Stackoverflow post:
How do you use the Java word boundary with apostrophes?
Try to put an word boundary before the words,
(?:\bthey\b|\bhe\b|\bshe\b|\bher\b|\bhim\b|\bthem\b|\bit\b)
Explanation:
(?:...) # Non captuaring groups
\b # Word boundary(It matches between a word character and a non word character)
DEMO
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What is a non-capturing group in regular expressions?
(18 answers)
What does ?! mean?
(3 answers)
Closed 2 years ago.
Like the regexp in this one? What does it match?
document.getElementById("MyElement").className =
document.getElementById("MyElement").className.replace
( /(?:^|\s)MyClass(?!\S)/ , '' )
?: means make the capturing group a non capturing group, i.e. don't include its match as a back-reference. This is often done to increase performance and de-clutter the back-references when a capturing group is necessary to use the | operator.
In your example, it is being used to allow the or (|) of the start of the string ^ or whitespace (\s). Since the author of this code doesn't care about what it matched, they have made it a non capturing group.
?! is the negative lookahead. The regex will only match if the capturing group does not match.
In this example, the author wants to ensure the character after MyClass is not a whitespace character (\S).
It is somewhat possible the author of this code could have used word boundaries instead (\b).
The regular expression (?:^|\s) is a non-capturing group that matches either the start of the line or a whitespace character.
The regular expression (?!\S) is a negative lookahead assertion which succeeds either at the end of the string, or else when the next character is a whitespace character.