[英]Extract information from a string using REGEX?
I have: 我有:
var str = 'WEB-3545 Detta är ett REGEX test WEB-3545';
I want: 我想要:
['WEB', '3545'];
Tried something like: 尝试过类似的内容:
str.match(/^(WEB-|DR-)\d+\s/)
The pattern I'm matching against is: String should start with: WEB- OR DR- Then follow with 1 or more numbers End with a space 我要匹配的模式是:字符串应以:WEB-或DR-开头,然后以1个或多个数字开头,以空格结尾
Ex; 例如;
So, in the example above only the highlighted part should match: ' WEB-3545 Detta är ett REGEX test WEB-3545' 因此,在上面的示例中,仅突出显示的部分应匹配:' WEB-3545 Dettaätett REGEX测试WEB-3545'
I can only run regex operations, not creating a loop or anything - because this is used in a Maven POM-file. 我只能运行正则表达式操作,不能创建循环或其他任何东西-因为它在Maven POM文件中使用。
Both Java and JavaScript solutions are fine. Java和JavaScript解决方案都很好。
Thanks! 谢谢!
/J / J
You can use regex ^\\s*(WEB|DR)-(\\d+)\\b
and use splice()
to remove the string match, since only need is capturing group 您可以使用regex
^\\s*(WEB|DR)-(\\d+)\\b
并使用splice()
删除字符串匹配项,因为只需要捕获组
var str = 'WEB-3545 Detta är ett REGEX test WEB-3545'; var res = str.match(/^\\s*(WEB|DR)-(\\d+)\\b/).splice(1); document.write(JSON.stringify(res));
The regex should be /^(WEB|DR)-(\\d+)\\b/
, \\b
matches the word edge, no need to use \\s
:) 正则表达式应为
/^(WEB|DR)-(\\d+)\\b/
, \\b
与单词edge匹配,无需使用\\s
:)
var reg = /^(WEB|DR)-(\d+)\b/;
var str = 'WEB-3545 Detta är ett REGEX test WEB-3545';
console.log(str.match(reg).slice(1));//remove input match
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.