首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用正则表达式获取字符串中的字母数字或数值?

如何使用正则表达式获取字符串中的字母数字或数值?
EN

Stack Overflow用户
提问于 2017-05-30 13:19:58
回答 4查看 113关注 0票数 1

我想获取字符串中的一项,

主要字符串是:This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting

我想获取ABX16059636/903213712,,有什么方法可以用Regex来实现吗?

请分享一些建议。

EN

回答 4

Stack Overflow用户

发布于 2017-05-30 13:27:56

尝试使用以下正则表达式,

代码语言:javascript
运行
复制
var string = "This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting"

var result = string.match(/[A-Z]+[0-9]+\/[0-9]+/g)

console.log(result)

票数 2
EN

Stack Overflow用户

发布于 2017-05-30 13:30:41

代码语言:javascript
运行
复制
var s = 'This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting'
var pat = /[A-Z]{3}\d+\/\d+/i
pat.exec(s)

这个正则表达式匹配任何3个字母,后面跟着一个或多个数字,然后是/,然后是一个或多个数字。

票数 1
EN

Stack Overflow用户

发布于 2017-05-30 13:35:52

尝试代码below.It将显示您的匹配项以及匹配组。

代码语言:javascript
运行
复制
const regex = /[A-Z]+[0-9]+\/+[0-9]+/g;
const str = `This is an inactive AAA product. It will be replaced by replacement AAAA/BBBB number ABX16059636/903213712 during quoting`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Live Demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44253998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档