This question already has answers here:
Javascript reg ex to match whole word only, bound only by whitespace
(5 answers)
Closed 6 years ago.
I have a string like this:
Free-coffee and coffee-free is free coffee
I need to match and replace only alone word free but not words {Free}-coffee and coffee-{free}
Idea is to mark bad words inside string and add HTML tag <strong> like this:
Free-coffee and coffee-free is <strong>free<strong> coffee.
I try with space but sometimes fail if before this sentence have space.
Here is my current regex:
/(\sfree\s|\sfree|free\s)/ig
NOTE: This need to be case insensitive.
And here is example of code:
var text = "Free-coffee and coffee-free is free coffee";
text = text.replace(/(\sfree\s|\sfree|free\s)/ig, " <strong>strong</strong> ");
Help me please.
Use the following regex pattern:
var text = "Free-coffee and coffee-free is free coffee";
text = text.replace(/(^|\s)(free)(\s|$)/ig, "$1<strong>$2</strong>$3");
console.log(text);
(^|\s) - ensures that free word is either at the start of the string OR preceded by space
(\s|$) - ensures that free word is either at the end of the string OR followed by space
Related
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 answers here:
Regular expression for all printable characters in JavaScript
(5 answers)
Closed 5 years ago.
I am trying to write a Reg Expression to check if the Language is English or Arabic.
My field under test is used to capture SMS messages. The message can be in
English with numbers & special characters
OR
Arabic with English numbers/Arabic numbers & special characters
Search should be on multiline accepting space & enter.
Check is to allocate the numbers of characters permissible per language. Eg: English allows 160; while Arabic allows only 70 per SMS
I assume the Exp should only check the words (first few to decide the language)
here is a sample of what I wrote in JavaScript; Regex did not work, only RegExp :
var pat = new RegExp("^[A-Za-z0-9\s!##$%^&*()_+=-`~\\\]\[{}|';:/.,?><]*$");
But for the below string it fails :
"Hello & Hi"
Any suggestions?
var regex=/^[ -~]+$/;
var str='Hello & Hi';
console.info(regex.test(str));
you can write like this too
Since you are creating the regular expression from string you need to escape \ character to use \s. Also, you should either escape - or put it just before closing ] when you are not using it to define a range of characters.
var re = new RegExp("^[A-Za-z0-9\\s!##$%^&*()_+=\-`~\\\]\[{}|';:/.,?><]*$");
console.log(re.test("Hello & Hi"));
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.