[英]Extract hex-code data from custom string
For example we put the following string: #FF00FFNick#AA00efName
例如,我们输入以下字符串: #FF00FFNick#AA00efName
I want to create a pattern to get the following output array 我想创建一个模式以获取以下输出数组
{ [0] = {"#FF00FF", "Nick"}, [1] = {"#AA00ef", "Name"} }
I write the following code 我写下面的代码
var reg = /#([a-f\d]{3}){1,2}(.*?)/gi;
alert(str.match(reg));
But the output i get only hex-code substrings. 但是输出我只得到十六进制代码子字符串。 Where is the mistake? 错误在哪里?
I suggest 我建议
/(#(?:[a-f\d]{3}){1,2})([^#]+)/gi
See the regex demo 见正则表达式演示
Details : 详细资料 :
(#(?:[af\\d]{3}){1,2})
- Group 1 capturing (#(?:[af\\d]{3}){1,2})
-组1捕获
#
- a hash symbol #
-井号 (?:[af\\d]{3}){1,2}
- 1 or 2 sequences of hex chars (case insensitive due to i
modifier) (?:[af\\d]{3}){1,2}
-1或2个十六进制字符序列(由于i
修饰符,不区分大小写) ([^#]+)
- Group 2 capturing 1+ chars other than #
. ([^#]+)
-组2捕获除#
之外的1个以上字符。 Demo: 演示:
var s = "#FF00FFNick#AA00efName"; var re = /(#(?:[af\\d]{3}){1,2})([^#]+)/gi; var res = [], m; while ((m=re.exec(s)) !== null) { res.push([m[1], m[2]]); } console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.