I need to make the condition, that will grab the color codes in format #111;
or #111111;
from string str
. If be more specifically, I need to grab only color codes with length: 3 or 6 after ' # ', any other codes need to be cut off.
var str = "color: #3f3; background-color: #AA00ef; and: #abcd;"; var reg = /#.{3}?{6};/; // for now I'm getting an error: // Invalid regular expression: Nothing to repeat console.log(str.match(reg));
You could take a group with either three or six characters and a global flag.
var str = "color: #3f3; background-color: #AA00ef; and: #abcd;", reg = /#(.{3}|.{6});/g; console.log(str.match(reg));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.