This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I have constructed a string as follows
let str = String.fromCharCode(13) + ' ';
Next, I would like to know if the string contains two spaces
str.match(/\s\s/)
The weird thing is that it is a match. But if I do
str.match(/ /)
it isn't. Can someone explain to me why this is happening ?
The '\s' pattern allows you to match any kind of whitespace, not just space itself. For a more detailed list you can check here for example.
For reference (copied from the developer reference):
Matches a single white space character, including space, tab, form
feed, line feed and other Unicode spaces. Equivalent to [
\f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
For example, /\s\w*/ matches " bar" in "foo bar".
Related
This question already has answers here:
Meaning of javascript text between two slashes
(3 answers)
Closed 2 years ago.
Stumbling upon a piece of JavaScript in a library I found this:
let useBlobFallback = /constructor/i.test(window.HTMLElement) || !!window.safari || !!window.WebKitPoint
but I can't find the meaning of the /constructor/i. Even searching online produces meaningless results because of the 'constructor' word and/or because the slash is also used in regular expressions. Which I believe it's not the case in this code snippet..
This is a RegExp literal. It's equivalent to new RegExp('constructor', 'i').test(window.HTMLElement).
Have a look at this maybe?
Simple patterns are constructed of characters for which you want to find a direct match. For example, the pattern /abc/ matches character combinations in strings only when the exact sequence "abc" occurs (all characters together and in that order). Such a match would succeed in the strings "Hi, do you know your abc's?" and "The latest airplane designs evolved from slabcraft." In both cases the match is with the substring "abc". There is no match in the string "Grab crab" because while it contains the substring "ab c", it does not contain the exact substring "abc".
This question already has answers here:
Matching a space in regex
(10 answers)
Closed 3 years ago.
I made a regex to highlight keywords found in a text by another tool.
new RegExp(highlightedKeywords.map(v => `\\b${v}\\b`).join('|') || /.^/, 'gi'), match => `<mark>${match}</mark>`)
I would like to make this regex treat every whitespaces as the same because the tool who extract keywords from the text convert every whitespace as space, so for example I have a keyword "the cat" not found because the actual text is "the\ncat".
I don't want to ignore whitespaces because "the cat" should not match "thecat" but I would like to match "the\tcat" or even "the\n \tcat"
Answer found thanks to pwilcox : "You're looking for \s"
new RegExp(highlightedKeywords.map(v => `\\b${v.replace(/\s/g, '\\s')}\\b`).join('|') || /.^/, 'gi')`
I replace every whitespace in the keywords by \s
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I've got a bunch of strings to browse and find there all words which contains "(at)" characters and then gather them in the array.
Sometimes is a replacement of "#" sign. So let's say my goal would be to find something like this: "account(at)example.com".
I tried this code:
let gathering = myString.match(/(^|\.\s+)((at)[^.]*\.)/g;);
but id does not work. How can I do it?
I found a regex for finding email addresses in text:
/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)
I think about something similar but unfortunately I can't just replace # with (at) here.
var longString = "abc(at).com xyzat.com";
var regex = RegExp("[(]at[)]");
var wordList = longString.split(" ").filter((elem, index)=>{
return regex.test(elem);
})
This way you will get all the word in an array that contain "at" in the provided string.
You could use \S+ to match not a whitespace character one or more times and escape the \( and \):
\S+\(at\)\S+\.\w{2,}
This question already has answers here:
javascript regex - look behind alternative?
(8 answers)
Closed 6 years ago.
I'm converting a python script I wrote to javascript. My python script has the following regex to match single instances of '\':
re.sub(r'(?<!\\)(\\{1})(?!\\)', r'\\', word)
I got a compiler error when trying to run this in js:
"Invalid regular expression: /(?<!\\)(\\{1})(?!\\)/: Invalid group"
After some searching found out that regex in js does not support look behinds.
I looked at this answer and they used:
^(?!filename).+\.js
in the form of a negative look-ahead from the start of the string, which does not help me as I need to change '\' to '\\' anywhere in the string.
I do not think this is a duplicate question as my question is trying to determine how to avoid and match the same character at different points in a string, while the linked question seeks to avoid a specific phrase from being matched.
I need to match '\' characters that do not have '\' either before or after them.
You always can use capture groups instead of lookbehind
string.match(/(^|[^\\])(\\{1})(?!\\)/)[2]
let replaced = "a\\b\\\\".replace(/(^|[^\\])(\\{1})(?!\\)/, x => x[0] == '\\' ? x : 'value')
console.log(replaced)
will return you same thing as (?<!\\)(\\{1})(?!\\)
Just match without assertions (^|[^\\])\\([^\\]|$) then substitute them back.
Note that this will tell you nothing about if it is escaping anything or not.
That regex is more complex.
This question already has answers here:
Regex to replace multiple spaces with a single space
(26 answers)
Closed 8 years ago.
So i have a dubt
if for example i have:
var string = "The String";
but i want string always to look "The String" (only 1 blank space in case of multiple sequenze of them)
how to do that in a clever way and dynamically, i mean there are many cases like these:
string = "This String";
string = "This String is short";
string = "This is the string";
i'm totally dumb in regexp (not only on it) and i guess it is the only way uh?
You should use a regex to get all spaces and replace it with one
string.replace(/\s\s+/g, " ");
If you only want it to work on a space and not tabs, use this:
string.replace(/ +/g, " ");
In the regex world "+" means 1 and any more that follow it. The "g" at the end means "global", or do it more than once. Removing the g would replace the first string of spaces but not any others. "\s" means all space-type characters which includes " " and tabs.