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
Related
This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed last month.
Im trying to build a scoreboard where you cannot input a name if it has special characters or if it contains spaces.
Tried something like this
const name = nameInput.value;
const test = /[A-z 0-9 ^\s]/.test(name)
But it doesn't work.
^ to match start and $ to match end,
/^[a-z0-9]+$/i.test("test"); => true
/^[a-z0-9]+$/i.test("test9"); => true
/^[a-z0-9]+$/i.test("test9 "); => false
/^[a-z0-9]+$/i.test("testæøå"); => false
ofc if you need unicode support, it gets a whole lot harder, not sure how to solve that.. (sure you could change the regex to /^[a-z0-9æøå]+$/iu to support Norwegian unicode characters, but that still leaves out Swedish unicode characters, you could add them but that would still leave out Chinese characters, and so on...)
i could make a unicode alnum table, but that would be huge, and would probably take a couple of days..
If you're ok with with an underscore (_), this should also do:
/^\w+$/.test("abiAcd9")
\w matches [A-Za-z0-9_]
This question already has answers here:
Javascript dynamic regex
(2 answers)
Closed 7 months ago.
const obj = {text: "The\bNew stylish \biPhone"}
I would like to replace all words in the above object that starts with '\b' followed by a single word with bold html tag with the identified word in the tag
i.e. text: "The\bNew stylish \biPhone" should be text: "The <b>New</b> stylish <b>iPhone</b>"
Thanks
You can capture \\b(\S+) and replace it with < b>$1< /b >.
Here \\b matches literal \b and (\S+) matches any word or text in general and gets captured in group1 which you can use to replace the matched text with bold tags.
const s = 'The \\bNew stylish \\biPhone';
console.log(s.replace(/\\b(\S+)/g, '<b>$1</b>'))
Just in case you want to limit any character set matching the world, change \S+ to something like \w or [a-zA-Z] and so on.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I am making a filter to block bad words and I am looking for a way to search a block of text for a word then replace the vowels of that word with * Is there a way to do this with a regex expression.
Using String.prototype.replace()
You could .join("|") an Array of bad words into a string like bad|awful|sick where the pipe | will act as a RegExp list of alternatives.
const text = "This is nice and I really like it so much!";
const bad = ["much", "like", "nice"].join("|");
const fixed = text.replace(new RegExp(`\\b(${bad})\\b`, "ig"), m => m.replace(/[aeiou]/ig, "*"));
console.log(fixed);
To replace the entire word with * use:
text.replace(new RegExp(`\\b(${bad})\\b`, "ig"), m => "*".repeat(m.length));
If you want to also target compound words (i.e: unlike given like is a bad word) than use simply RegExp(bad, "ig") without the \b Word Boundary assertion.
Also, if necessary escape your bad words if some contain RegExp special characters like . etc...
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 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".