Skip to main content
  1. About
  2. For Teams
Asked
Viewed 669 times
0

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?

3 Answers 3

5

Regex:

result = subject.replace(/[^abc]/ig, "<span style='color:red;'>$&</span>");
Sign up to request clarification or add additional context in comments.

1 Comment

@qwertymk, It matches the matched string. I've always loved: gskinner.com/RegExr
3
var strn= "aaabc8abccc";
var chrs = 'abc';
strn=strn.replace(new RegExp('([^'+chrs+'])','g'),'<span style="color:red">$1</span>');

1 Comment

I'm not gonna lie, there was some initial turbulence with the code, but it works now.
-2

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>")

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.