Phrase search in string, making correct RegExp

后端 未结 1 1063
無奈伤痛
無奈伤痛 2021-01-15 04:13

I will start straight, here\'s what I have:

var SResults = [];
function ActivateSearch(s) {
    SResults = [];

    for (var key in Products){
        if ((\         


        
相关标签:
1条回答
  • 2021-01-15 05:18

    Another option would have been to split and use alternation with positive lookaheads and word boundaries, such as:

    ^((?=.*\btrampoline\b)|(?=.*\bcover\b)).*$ 
    

    const regex = /^((?=.*\btrampoline\b)|(?=.*\bcover\b)).*$/gmi;
    const str = `Trampoline rain cover 6ft lenght plus extras
    Rain cover 6ft lenght plus extras
    Trampoline rain 6ft lenght plus extras
    Cover trampoline rain 6ft lenght plus extras
    Covers trampolines rains 6ft lenght plus extras
    `;
    let m;
    
    while ((m = regex.exec(str)) !== null) {
    	m.forEach((match, groupIndex) => {
    		if (groupIndex == 0)
    			console.log(`Found match, group ${groupIndex}: ${match}`);
    	});
    }


    If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


    0 讨论(0)
提交回复
热议问题