2

I've got a string with various hex values in random places.

Is it possible to replace all hex values to a single value #FF0000 ?

var info_str = 'The avocado, also known as butter pear or alligator pear, is a fruit that is widely "acknowledged to #E5E5E5 have properties" that reduce cholesterol levels... also #00FF00 ...';

I need to replace all hex values #xxxxxx with #FF0000. How can I do that?

replace() does not work because the hex values in the string are different.

1
  • where is the text written into span or p or div? can you specify? Commented Sep 3, 2015 at 8:07

4 Answers 4

4

You can use a simple regex like

var info_str = 'The avocado, also known as butter pear or alligator pear, is a fruit that is widely "acknowledged to #E5E5E5 had (property" that reduce cholesterol levels... also #00FF00 ...';

var str = info_str.replace(/#[\da-z]+/ig, '#FF0000');

snippet.log(str)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Sign up to request clarification or add additional context in comments.

4 Comments

As per Pranav C Balan's answer, what's the difference between #[\da-z]+ and #[\da-f]{6} ?
@Becky the one will check 6 digits, and the other will not have a limit on the numbers ie. #1234567899 will be matched.
@Becky that will make sure there is 6 hexa characters following #... mine just checks whether 1 or more characters is there
@MrNeo g for global match and i for caseinsensitive match. You can check my answer for more description stackoverflow.com/a/32370230/2025923
3

Use replace() with regex as /#[\da-f]{6}/ , also add flags ig for ignoring case and global match

var info_str = 'The avocado, also known as butter pear or alligator pear, is a fruit that is widely "acknowledged to #FFF001 have properties" that reduce cholesterol levels... also #00FF00 ...';
document.getElementById('myDiv').innerHTML = info_str.replace(/#[\da-f]{6}/ig, '#FF0000');
<div id="myDiv"></div>

Explanation :

#[\da-f]{6}
  1. # matches the character #
  2. \d for matching any digit
  3. a-f for matching letters a,b,c,d,e or f, since hex value contains only these alphabets
  4. {6} Exactly 6 times

Regular expression visualization

Debuggex Demo

Regex demo

6 Comments

Why da-f ? and not da-z ?
@Becky it's \d Matches any digit and a-z matches any alphabet from a to z. As the hex are from a - f, there's no need of using a-z
@Becky The alphabets after f are not valid HexaDecimal Characters. Only a to f are valid for Hex value
How can I write the full updated string in to a div? document.getElementById('myDiv').innerText = info_str; ?
document.getElementById('myDiv').innerHTML = info_str
|
3

You can use regex to replace all the hex values from the string.

var info_str = 'The avocado, also known as butter pear or alligator pear, is a fruit that is widely "acknowledged to #E5E5E5 have properties" that reduce cholesterol levels... also #00FF00 ...';

var replaced = info_str.replace(/#[a-f0-9]{6}/gi, '#FF0000');

document.write(replaced);

Regex Explanation

  1. #: Matches # literal
  2. [a-f0-9]: Matches any of the range from a to f and 0 to 9
  3. {6}: Matches previous character class 6 times
  4. gi: Global and incasesensitive match.

Regex Visualization

Regex Visualization

Comments

2

Yes using this regex /#[A-F0-9]{6}/gi with replace() function:

var info_str = 'The avocado, also known as butter pear or alligator pear, is a fruit that is widely "acknowledged to #E5E5E5 have properties" that reduce cholesterol levels... also #00FF00 ...';

var replaced = info_str.replace(/#[A-F0-9]{6}/gi, "#FF0000");

document.write("<b>"+replaced+"</b>");

This will match any Hex value and replace it.

Explanation:

  • # matches the character # literally.
  • A-F a single character in the range between A and F.
  • 0-9 a single character in the range between 0 and 9.
  • Quantifier: {6} Exactly 6 times.
  • g modifier: global. All matches (don't return on first match).

3 Comments

WHat's the difference between the regex of yours and Balan's answer?
@Becky They are similar, he used \d for numbers and I used 0-9, but they will lead to the same result, and we use A-F here because Hexadecimals can contain only A, B, C, D, E or F.
Great, and the {6} is telling that the hexadecimal will have exactly 6 chracters after #.

Your Answer

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.