Reputation: 9096
For example, I have an approved list of characters: "a", "b", and "c".
So if I had the string:
var string = "aaabc8abccc";
I would like the script to detect the fact that the "8" is not "a", "b", or "c" and output:
var output = "aaabc<span style='color:red;'>8</span>abccc";
How can I do this?
Upvotes: 0
Views: 664
Reputation: 1067
You can do it with regular expressions using string.replace(regexp/substr,newstring)
In your case it will be something like
string.replace(/^[a-z]*/,"<span>$1</span>")
Upvotes: -2
Reputation: 17508
Regex:
result = subject.replace(/[^abc]/ig, "<span style='color:red;'>$&</span>");
Upvotes: 5
Reputation: 17382
var strn= "aaabc8abccc";
var chrs = 'abc';
strn=strn.replace(new RegExp('([^'+chrs+'])','g'),'<span style="color:red">$1</span>');
Upvotes: 3