supercoolville
supercoolville

Reputation: 9096

Check if string contains character not on list

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

Answers (3)

ProdoElmit
ProdoElmit

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

Ofer Zelig
Ofer Zelig

Reputation: 17508

Regex:

result = subject.replace(/[^abc]/ig, "<span style='color:red;'>$&</span>");

Upvotes: 5

mowwwalker
mowwwalker

Reputation: 17382

var strn= "aaabc8abccc";
var chrs = 'abc';
strn=strn.replace(new RegExp('([^'+chrs+'])','g'),'<span style="color:red">$1</span>');

Upvotes: 3

Related Questions