Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  127] [ 7]  / answers: 1 / hits: 25270  / 15 Years ago, thu, january 28, 2010, 12:00:00

Greetings JavaScript and regular expression gurus,



I want to return all matches in an input string that are 6-digit hexadecimal numbers with any amount of white space in between. For example, 333333 e1e1e1 f4f435 should return an array:



array[0] = 333333  
array[1] = e1e1e1
array[2] = f4f435


Here is what I have, but it isn't quite right-- I'm not clear how to get the optional white space in there, and I'm only getting one match.




colorValuesArray = colorValues.match(/[0-9A-Fa-f]{6}/);




Thanks for your help,



-NorthK


More From » regex

 Answers
10

Use the g flag to match globally:



/[0-9A-Fa-f]{6}/g


Another good enhancement would be adding word boundaries:



/b[0-9A-Fa-f]{6}b/g


If you like you could also set the i flag for case insensitive matching:



/b[0-9A-F]{6}b/gi

[#97728] Sunday, January 24, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahk

Total Points: 166
Total Questions: 94
Total Answers: 117

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;