Replacing using regex in a string javascript - javascript

I am trying to replace a particular string using regex.
var replace = {'<RAndom>': "random object"};
I am replacing it using the dynamic regex because i have a lot of objects.
var tagsText = "<RAndom> hellow world";
var regex = new RegExp('\\b(' + Object.keys(replace).join('|') + ')\\b', 'g');
tagsText = tagsText.replace(regex, function(match) {
return replace[match] + match;
});
But it is not working.I think the problem is with the semicolon but i am not sure.The output is again the same.
"<RAndom> hellow world"
Any ideas?

Problem is presence of \b (word boundary) on each side that is placed before & and ;. Both & and ; are not non-word characters and \b cannot be asserted before and after non-word chars.
You can use \B instead:
var regex = new RegExp('\\B(' + Object.keys(replace).join('|') + ')\\B', 'g');
and then
tagsText = tagsText.replace(regex, function(match) {
return replace[match] + match;
});
//=> "random object<RAndom> hellow world"

The word boundary \b and non-word boundary assertion behavior depends on the context. Make it context-independent with unambiguous (^|\W) and ($|\W):
var replace = {'<RAndom>': "random object"};
var tagsText = "<RAndom> hellow world";
var regex = new RegExp('(^|\\W)(' + Object.keys(replace).join('|') + ')(\\W|$)', 'g');
tagsText = tagsText.replace(regex, function(match, g1, g2, g3) {
return replace[g2] ? replace[g2] + match : match;
});
// And just a demo below
document.body.innerHTML = "<pre>" + tagsText.replace(/&/g, '&') + "</pre>";
The (^|\W) will match the start of string or a non-word character. The ($|\W) will match the end of the string or a non-word character.
Since we have 3 groups now, we can pass them as arguments to the replace callback. With replace[g2] ? replace[g2] + match : match;, we first check if there is a value for g2 key, and if yes, perform the replacement. Else, just return the match.

Related

Regex word search with apostrophe

highlightStr: function (body, searchString){
console.log(searchString);
var regex = new RegExp('(' + searchString + ')', 'gi');
console.log(regex)
return body.replace(regex, "<span class='text-highlight'>$1</span>");
}
Above is the code I'm using. I want to find and replace the searchString, which could be anything. It works fine for most words, but fails when finding words with apostrophes.
How can I modify the regex to include special characters like the appostrophe.
var body = "<br>I like that Appleā€™s.<br>";
var searchString = "Apple's";
Thank you
You should escape the search string to make sure the regex works OK even if the search string contains special regex metacharacters.
Besides, there is no need wrapping the whole pattern with a capturing group, you may always reference the whole match with $& placeholder from the replacement pattern.
Here is an example code:
var s = "I like that Apple's color";
var searchString = "Apple's";
var regex = new RegExp(searchString.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "gi");
document.body.innerHTML = s.replace(regex, '<b>$&</b>');

Regular Expression Url Address

My URL String:
https://stackoverflow.com/questions
My regex : [\w.]+
Result: ["http","stackoverflow.com","questions"]
How I ignore .* in stackoverflow.com only one regex.
Result I want: ["http","stackoverflow","questions"]
You can use this regex that captures into Group 1 all alphanumeric/underscore chunks that are not preceded with .:
/(?:^|[^.])\b(\w+)\b/g
See the regex demo.
Breakdown:
(?:^|[^.]) - matches (but does not store in a group buffer) the beginning of a string or any character but a literal dot
\b - leading word boundary
(\w+) - Group 1 capturing one or more word characters
\b - trailing word boundary
Sample code:
var re = /(?:^|[^.])\b(\w+)\b/g;
var str = 'http://stackoverflow.com/questions';
var res = [];
while ((m = re.exec(str)) !== null) {
res.push(m[1]);
}
document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";
Another solution based on the assumption the word character should not be followed with /:
\b\w+\b(?!\/)
See another regex demo
var re = /\b\w+\b(?!\/)/g;
var str = 'http://stackoverflow.com/questions';
var res = str.match(re);
document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";
Note that both solutions require a word boundary to work properly, just a negated character class (#1) or a lookahead (#2) won't work by themselves (partial matches will be rejected thanks to \b).

String that doesn't contain character group

I wrote regex for finding urls in text:
/(http[^\s]+)/g
But now I need same as that but that expression doesn't contain certain substring, for instance I want all those urls which doesn't contain word google.
How can I do that?
Here is a way to achieve that:
http:\/\/(?!\S*google)\S+
See demo
JS:
var re = /http:\/\/(?!\S*google)\S+/g;
var str = 'http://ya.ru http://yahoo.com http://google.com';
var m;
while ((m = re.exec(str)) !== null) {
document.getElementById("r").innerHTML += m[0] + "<br/>";
}
<div id="r"/>
Regex breakdown:
http:\/\/ - a literal sequence of http://
(?!\S*google) - a negative look-ahead that performs a forward check from the current position (i.e. right after http://), and if it finds 0-or-more-non-spaces-heregoogle the match will be cancelled.
\S+ - 1 or more non-whitespace symbols (this is necessary since the lookahead above does not really consume the characters it matches).
Note that if you have any punctuation after the URL, you may add \b right at the end of the pattern:
var re1 = /http:\/\/(?!\S*google)\S+/g;
var re2 = /http:\/\/(?!\S*google)\S+\b/g;
document.write(
JSON.stringify(
'http://ya.ru, http://yahoo.com, http://google.com'.match(re1)
) + "<br/>"
);
document.write(
JSON.stringify(
'http://ya.ru, http://yahoo.com, http://google.com'.match(re2)
)
);

How to define word boundry with chars like #:.[ in regex on javascript

I want to search this word: '#foo'
in this string:
first foo second #foo
so that only the last '#foo' (with '#') will match
I use this code and it causes two mistakes:
var mySearch ='#foo'
var regexp = new RegExp('\\b' + mySearch + '\\b', 'g');
searching mySearch = 'foo' will result a match to both "foo" (wrong! - should match only the first)
searching mySearch = '#foo' will result no hit at all (wrong - should match the second)
how to write it right?
Try:
var regexp = new RegExp('\b' + mySearch + '\b', 'g');
\b in regex is boundary of word. It will fail to match the boundary between a space and a pound #.
So your regexp should just be
var regexp = new RegExp(mySearch + '\\b', 'g');

Split String at specific character

I have the following code:
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
The function takes no regard to whitespace. For example if you input
ends_with('Jonas Sand,', ',')
the output will be Sand. Need help on making the function work with words that has whitespace.
You can use your separator within split and take all except the last part with slice:
function ends_with(string, character) {
return string.split(character).slice(0, -1);
}
\w matches word characters, use [^x] instead, where x is your character. This matches everything but your character.
So the first line in your function becomes
var regexp = new RegExp('[^' + character + "]+" + character, 'g');
on the other hand, if you want to match words separated by white space, use
var regexp = new RegExp('(\\w|\\s)+" + character, 'g');
PS: but isn't there a String#split function in javascript?
Try using '[\\w\\s]+' instead of '\\w+' to include whitespace.
Try the following:
var string = "word1;w ord2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('.+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}

Categories

Resources