JavaScript匹配逗号分隔的字符串中的多个值

时间:2019-06-03 12:33:30

标签: javascript regex

嗨,我有一个带有一串数字的字符串

var matches = '128,126,125,124,123,122,118,117,116,115,99,98,97';

,如果值与上面提供的字符串中的匹配,我想用其他子字符串检查

var numbers = '126,125';

我尝试过正则表达式

if( $(this).attr('data-ids').match(new RegExp("(?:^|,)"+matches+"(?:,|$)"))){console.log('found'); 
}else {console.log('not found')}

但是正则表达式适用于单个值,不适用于多个值

var numbers = '125';

我希望它可以处理单个和多个匹配项。

5 个答案:

答案 0 :(得分:1)

如果顺序不重要(numbers可能是125,126 o4 126,125,并且您想两者都匹配您的示例),则需要将这些字符串拆分为数组,然后检查numbers中的条目是否全部在matches中:

const match = numbers.split(",").every(n => matchesArray.includes(n));

实时示例:

function test(matches, numbers, expected) {
    const matchesArray = matches.split(",");
    const match = numbers.split(",").every(n => matchesArray.includes(n));
    console.log(`matches: ${matches}`);
    console.log(`numbers: ${numbers}`);
    console.log(`match:   ${match}`);
    console.log(!match === !expected ? "=> Good" : "=> ERROR");
}

test(
    '128,126,125,124,123,122,118,117,116,115,99,98,97',
    '126,125',
    true
);

test(
    '128,126,125,124,123,122,118,117,116,115,99,98,97',
    '125,126', // <== I changed the order here
    true
);

test(
    '128,126,125,124,123,122,118,117,116,115,99,98,97',
    '119,126',
    false
);
.as-console-wrapper {
    max-height: 100% !important;
}

如果顺序很重要,最简单的方法是在它们周围加上逗号并进行子字符串检查:

const match = ("," + matches + ",").includes("," + numbers + ",");

实时示例:

function test(matches, numbers, expected) {
    const match = ("," + matches + ",").includes("," + numbers + ",");
    console.log(`matches: ${matches}`);
    console.log(`numbers: ${numbers}`);
    console.log(`match:   ${match}`);
    console.log(!match === !expected ? "=> Good" : "=> ERROR");
}

test(
    '128,126,125,124,123,122,118,117,116,115,99,98,97',
    '126,125',
    true
);

test(
    '128,126,125,124,123,122,118,117,116,115,99,98,97',
    '125,126', // <== I changed the order here
    false      // <== Shouldn't work
);
.as-console-wrapper {
    max-height: 100% !important;
}

答案 1 :(得分:1)

如果顺序无关紧要,则可以拿一套检查每个想要的物品。

var matches = '128,126,125,124,123,122,118,117,116,115,99,98,97',
    numbers = '126,125',
    has = numbers.split(',').every(Set.prototype.has, new Set(matches.split(',')));

console.log(has);

答案 2 :(得分:1)

您需要在match中切换顺序。我假设numbers变量具有$(this).attr('data-ids')返回的值。您需要检查$(this).attr('data-ids')内部是否存在matches,而不是相反。更改为:

if (matches.match(new RegExp("(?:^|,)" + $(this).attr('data-ids') + "(?:,|$)"))) {
  console.log('found');
} else {
  console.log('not found')
}

此外,如果您只想检查子字符串是否存在,则可以改用RegExp#test

答案 3 :(得分:-1)

您可以includesevery

var matches = '128,126,125,124,123,122,118,117,116,115,99,98,97';

var numbers = '126,125';

var t_m = matches.split(',')
var t_n = numbers.split(',')

t_n.every(num => t_m.includes(num)) ? console.log(true) : console.log(false)

如果顺序很重要,您可以找到第一个数字的索引,并检查后面的每个数字是否跟随:

var matches = '128,126,125,124,123,122,118,117,116,115,99,98,97';

var numbers = '126,125';

var t_m = matches.split(',')
var t_n = numbers.split(',')

var index = t_m.indexOf(t_n[0])
t_n.every((num, i) => num === t_m[i + index]) ? console.log(true) : console.log(false)

答案 4 :(得分:-1)

尝试

(','+matches+',').search(','+numbers+',')>=0;

var matches = '128,126,125,124,123,122,118,117,116,115,99,98,97',
    numbers = '126,125';
    
let r= (','+matches+',').search(','+numbers+',')>=0;

console.log(r);