找到所有单词以特定字符开头,后跟字符串中的数字

Aak*_*Kag 2 javascript regex jquery node.js

我想找到所有以mc开头并且后跟所有数字的单词

var myString="hi mc1001 hello mc1002 mc1003 mc1004 mc mca" 
Run Code Online (Sandbox Code Playgroud)

需要输出= [mc1001,mc1002,mc1003,mc1004]

我的解决方案

var myRegEx = /(?:^|\s)mc(.*?)(?:\s|$)/g;

function getMatches(string, regex, index) {
    index || (index = 1); // default to the first capturing group
    var matches = [];
    var match;
    console.log("string==",string)
    while (match = regex.exec(string)) {
        console.log("string==",string)
        matches.push(match[index]);
    }
    return matches;
}

var matches = getMatches(myString, myRegEx, 1);
console.log("matches===>",matches)
Run Code Online (Sandbox Code Playgroud)

面临的问题:我的代码只返回所有奇怪的possting单词我只使用节点js

Nin*_*olz 5

您可以搜索单词边界,然后搜索以下字母mc和一些数字,后跟另一个单词边界.

var string = "hi mc1001 hello mc1002 mc1003 mc1004 amc1005 mc mca mc1234a";

console.log(string.match(/\bmc\d+\b/g));
Run Code Online (Sandbox Code Playgroud)