简体   繁体   English

使用正则表达式从字符串中提取十六进制代码

[英]Extract hex code from string using regex

I am trying to pass a message using Node-Red (nodered.org) to a function. 我正在尝试使用Node-Red(nodered.org)将消息传递给函数。

So the message would be something like: Can I have 00ff00 please? 所以消息将是这样的: Can I have 00ff00 please?

I am only interested in the hex code value and I need to parse the message and extract the hex with regex. 我只对十六进制代码值感兴趣,我需要解析消息并用正则表达式提取十六进制。 This is the code I have: 这是我的代码:

var str = msg.payload;
var colorCode = str.match([A-Fa-f0-9]{6}/g);
return colorCode;

Something is not right and I get an error saying Unexpected token { 某事不正确,我收到一条错误消息,说“ Unexpected token {

It doesn't work even if I put [A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]/g I get an A is not defined error, probably because it doesn't consider it a regex. 即使我放了[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]/g我得到一个A is not defined错误,可能是因为它不认为它是正则表达式。

HEX color always starts from # and can has 3 or 6 digits value. 十六进制颜色始终从#开始,并且可以具有36位数字的值。 Try /^#[a-z0-9]{3}([a-z0-9]{3})?$/i : 试试/^#[a-z0-9]{3}([a-z0-9]{3})?$/i

'#fff000'.match(/^#[a-z0-9]{3}([a-z0-9]{3})?$/i);//["#fff000", "000"]
'#fff'.match(/^#[a-z0-9]{3}([a-z0-9]{3})?$/i);//["#fff", undefined]

Also you have a misprint in(you forgot regexp start slash), fixed regexp: 另外,您在(正确的正则regexp忘了正则regexp起始斜杠)打印有误:

/[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]/g

Shorter version for 6-digits only value: 较短版本的仅6位数字值:

'#fff000'.match(/^#[a-z0-9]{6}$/i);//["#fff000"]

Or multiple-color string check: 或多色字符串检查:

'#fff000 #aaabbb #ccc999'.match(/#[a-z0-9]{6}/gi);

You need to put / 您需要输入/

Use any of 使用任何

str.match(/[A-Fa-f0-9]{6}/) or str.match(/[A-Fa-f0-9]{6}/)

str.match(/[a-f0-9]{6}/i)

instead of str.match([A-Fa-f0-9]{6}) 而不是str.match([A-Fa-f0-9]{6})

Now if your string may contain multiple HEX codes then use the following instead: 现在,如果您的字符串可能包含多个十六进制代码,请改用以下代码:

str.match(/[a-f0-9]{6}/gi) -> This will fetch an array of all such HEX codes and hence you can access each such instance using index to the array as follows: str.match(/[a-f0-9]{6}/gi) ->这将获取所有此类十六进制代码的数组,因此您可以使用对数组的索引来访问每个此类实例,如下所示:

str="Can I have 00fA00 and B0fA0c please?"
hex_codes=str.match(/[a-f0-9]{6}/gi);
//hex_codes[0]=="00fA00" and hex_codes[1]=="B0fA0c"

Here is the fiddle demo 这是小提琴演示

As pointed on the comments, you've missed an / in front of your Regex to satisfy Javascript's Regex syntax. 正如评论中指出的那样,您已经错过了Regex前面的/来满足Javascript的Regex语法。

Also, I'd like to suggest you to put boundaries on your regex, just to avoid some weird scenarios (like catching "BABABABABA"). 另外,我想建议您在正则表达式上加些界限,以避免某些奇怪的情况(例如捕获“ BABABABABA”)。 Also, as pointed on the comments below, #FFF is a valid value for the color, so your Regex could be further improved. 另外,正如下面的注释所指出的那样,#FFF是该颜色的有效值,因此可以进一步改善您的Regex。 The result is the following: 结果如下:

/\b([a-f0-9]{3}|[a-f0-9]{6})\b/i

Catches: 渔获:

Can I have C0ffee please?
The color is #FFF

Doesn't catch: 没有抓住:

Testing 00000ff0000 just to be safe!
Making sure BABABABABA doesn't get catched also :)
Checking #ffff

As a final note, you may want to consider using the pattern #RRGGBB to catch those HEX colors, simply because you may find valid english words with 6 characters with letters from AF. 最后一点,您可能要考虑使用#RRGGBB模式来捕获那些十六进制颜色,仅仅是因为您可能会找到6个字符的有效英文单词以及来自AF的字母。 So, if you want to do that, just add an \\# in front of your regex: 因此,如果要这样做,只需在正则表达式前添加一个\\#

/\#\b([a-f0-9]{3}|[a-f0-9]{6})\b/i

Check this Regex101 , it contains some examples of some inputs. 检查此Regex101 ,其中包含一些输入的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM