I have a string in JavaScript in which I'd like to find all matches of a given phrase and wrap them with a tag. I haven't been able to find the right regex method here to replace a case insensitive phrase and replace it with itself with additional text around it. For example:
Input string:
"I like to play with cats, as does Cathy, who is a member of ACATA, which is the American Cat And Tiger Association."
Case insensitive phrase: "cat"
Output string:
"I like to play with <em>cat</em>s, as does <em>Cat</em>hy, who is a member of A<em>CAT</em>A, which is the American <em>Cat</em> And Tiger Association."
So, basically, inject <em></em> around any matches. I can't just do a straight-up replace, because I'll lose the original case in the input string.
You could use:
"Foo bar cat".replace(/(cat)/ig, "<em>$1</em>");
Which will return:
"Foo bar <em>cat</em>"
You can do straight-up replace by using a replace function:
str.replace(/cat/ig, function replace(match) {
return '<em>' + match + '</em>';
});
One needs no capturing group around the whole regex because there is a $& placeholder that refers to the whole match from the string replacement pattern:
"Foo bar cat".replace(/cat/ig, "<em>$&</em>");
^^
See the Specifying a string as a parameter section:
$& Inserts the matched substring.
Thus, no need of any callback methods or redundant regex constructs.
You can solve it very simple, just use a capturing group with the word you want, and then use the replacement string <em>$1</em>. You can use a regex like this:
(cat)
working demo
Below, you can see in green the matches (capturing the word cat insensitively) and in the substitution section you can see the replacements.
You can use this code:
var re = /(cat)/ig;
var str = '"I like to play with cats, as does Cathy, who is a member of ACATA, which is the American Cat And Tiger Association."\n';
var subst = '<em>$1</em>';
var result = str.replace(re, subst);
Related
I have a string in JavaScript in which I'd like to find all matches of a given phrase and wrap them with a tag. I haven't been able to find the right regex method here to replace a case insensitive phrase and replace it with itself with additional text around it. For example:
Input string:
"I like to play with cats, as does Cathy, who is a member of ACATA, which is the American Cat And Tiger Association."
Case insensitive phrase: "cat"
Output string:
"I like to play with <em>cat</em>s, as does <em>Cat</em>hy, who is a member of A<em>CAT</em>A, which is the American <em>Cat</em> And Tiger Association."
So, basically, inject <em></em> around any matches. I can't just do a straight-up replace, because I'll lose the original case in the input string.
You could use:
"Foo bar cat".replace(/(cat)/ig, "<em>$1</em>");
Which will return:
"Foo bar <em>cat</em>"
You can do straight-up replace by using a replace function:
str.replace(/cat/ig, function replace(match) {
return '<em>' + match + '</em>';
});
One needs no capturing group around the whole regex because there is a $& placeholder that refers to the whole match from the string replacement pattern:
"Foo bar cat".replace(/cat/ig, "<em>$&</em>");
^^
See the Specifying a string as a parameter section:
$& Inserts the matched substring.
Thus, no need of any callback methods or redundant regex constructs.
You can solve it very simple, just use a capturing group with the word you want, and then use the replacement string <em>$1</em>. You can use a regex like this:
(cat)
working demo
Below, you can see in green the matches (capturing the word cat insensitively) and in the substitution section you can see the replacements.
You can use this code:
var re = /(cat)/ig;
var str = '"I like to play with cats, as does Cathy, who is a member of ACATA, which is the American Cat And Tiger Association."\n';
var subst = '<em>$1</em>';
var result = str.replace(re, subst);
I want to remove << any words #_ from the following string.
stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ ";
Result mast be:
Start words I love kind <<man>>, john <<kind man>> is really <<great>> end words
I tried like this:
stringVal = stringVal.replace(/^.*<<.+\#_.*$/g, "");
But it removes all string.
Note: << any words #_ may exists multiple time in string, at the start, in the middle or at the end
Inferring from your examples, you might be looking for:
stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ ";
stringVal = stringVal.replace(/<<([-\w ]+)#_/g, "$1");
console.log(stringVal);
To allow other characters, change the \w+ to e.g. [-\w ]+.
See an additional demo on regex101.com.
Instead of using .+\#_, and you want to match any words you could match word characters optionally repeated by matching a space space and word characters.
<<(\w+(?: \w+)*)#_
Regex demo
In the replacement use group 1 $1
Note that you don't have to escape #
const regex = /<<(\w+(?: \w+)*)#_/g;
stringVal = "I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>>";
const result = stringVal.replace(regex, '$1');
console.log(result);
I want to find a sentence in an HTML doc. I know how it begins and ends but it could have different words/characters in the middle. Is it possible to use a combination of string and regex and then string?
const stringBegin = 'The film was';
const regex = /[AZaz09_]/g;
const stringEnd = 'in London';
$('p:contains(stringBegin,regex,stringEnd).text();
I guess this would work for you:
var html = $('html').text();
var matches = html.match(/The\sfilm\swas(.*)in\London/mi);
matches[0] will contain the whole matching text.
matches[1] will contain the text between 'The film was' and 'in London' (you might want to use matches[1].trim(), to remove starting and ending whitespaces).
You can use this regex pattern: /The film was .* in London./g
You can use a single regular expression to make the match, however your bigger problem is that you cannot use it with a :contains selector. Instead you can use filter() to provide a function with the logic to check each individual element. Try this:
var re = /The film was \w+ in London/gi;
var $p = $('p').filter(function() {
return re.test($(this).text());
});
// do something with $p here...
$p.addClass('foo');
.foo { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>The film was shot in London</p>
<p>The film was shot in California</p>
<p>The film was produced in London</p>
Also note the use of \w in the regex, which is a shorter form of the character set you originally had.
I have a simple regex search and replace function and was wondering if there is a good way to go a case sensitive replace on a case insensitive search? Example below:
function filter(searchTerm) {
var searchPattern = new RegExp(searchTerm, 'ig');
....
textToCheck = textToCheck.replace(searchPattern, '<span class="searchMatchTitle">' + searchTerm + '</span>');
The search term at the top of the function could have a capitol in it and I wanted to make it match any string with upper or lower in it, hence the 'i' flag.
I end up with weird results when doing the replace as it takes the original seach string (which could be any mix) and uses that a replacement where there might be different formatting.
Id really love to preserve the original formatting and add the span around it. Is my approach the best way? If it is, can you use groups in the same way you can in Perl? e.g.
s/ \(=\+\) /\1/g
My attempts using similar types of syntax and examples form the web only result in literal string replace
function filter(searchTerm) {
var searchPattern = new RegExp('('+searchTerm+')', 'ig');
....
textToCheck = textToCheck.replace(searchPattern, '<span class="searchMatchTitle">$1</span>');
...
}
You can use groups, but the syntax is slightly different. Try something like this:
var str = 'this is a dog eat Dog eat DOG world'
str = str.replace(/(dog)/gi, '<span>$1</span>')
// str is now: "this is a <span>dog</span> eat <span>Dog</span> eat <span>DOG</span> world"
How can I replace some words in a string with some other words? For example:
var text1 = "This is a sentence. It is a pencil."
text2 = modify(text1);
I want text2 to be "That was a sentence. I was a pencil."
So modify function replaces This->That , is->was
To replace all instances of the substring is with was you can use the replace[MDN] method:
text2 = text1.replace(/is/g, "was");
Note that because is is a part of the word this, it will actually return
Thwas was a sentence
If you wanted to replace all instances of This to That and is to was, you could chain the calls to the replace method.
text2 = text1.replace(/This/g, "That").replace(/is/g, "was");
This will correctly do your replacement from
This is a sentence. It is a pencil.
to
That was a sentence. It was a pencil.
You can see this in action on jsFiddle.
Note that find and replace actions like this can always have unintended consequences. For example, this string
Thistles and thorns are bad for missiles and corns.
will turn into this one after your replacement:
Thatles and thorns are bad for mwassiles and corns.
This sort of thing is popularly known as the Clbuttic mistake.
text1 = text1.replace('is', 'was');
Btw, .replace accepts regular expressions as well
Utilize the javascript replace method -
http://www.w3schools.com/jsref/jsref_replace.asp
Note: To replace every occurrence of a string in JavaScript, you must provide the replace() method a regular expression with a global modifier as the first parameter.
You could use javascript's Replace function like this:
var text2 = text1.replace('is','was');