xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js regex match multi groups All In One

js regex match multi groups All In One


const reg = /(^\[\w+\]$)/gi;



https://regexper.com/#%2F%28%5E%5C%5B%5Cw%2B%5C%5D%24%29%2Fg

https://regexper.com/#%2F%28%5E%5C%5B%5Cw%2B%5C%5D%24%29%2Fgi

js regex match multi groups

js 正则匹配多组标签


`[A][B][bug`.match(/(^\[\w+\]$)/g);
// null

`[A][B][bug`.match(/(\[\w+\])/g);
// (2) ["[A]", "[B]"]

js match group 动态词包


const str = '[节日]abc[男女]xyz[节日]???[他她';

const rel_names = ["[日期]", "[星期]", "[节日]", "[男性女性]", "[帅哥美女]", "[他她]", "[反性别-夫妻]", "[年龄范围]", "[热门手机品牌]", "[地区]"];

str.match(/(\[\W+\])/g);
// (3) ["[节日]", "[男女]", "[节日]"] ✅ 🚀 👍 🇨🇳 OK


image

const str = '[节日]abc[男女]xyz[节日]???[他她';

const rel_names = ["[日期]", "[星期]", "[节日]", "[男性女性]", "[帅哥美女]", "[他她]", "[反性别-夫妻]", "[年龄范围]", "[热门手机品牌]", "[地区]"];

str.match(/(\[.+\])/g);
// ["[节日]abc[男女]xyz[节日]"] ??? ❌ bug

\W 非单词字符 (中文字符)

\W 元字符用于查找非单词字符。

单词字符包括:a-z、A-Z、0-9,以及下划线。

https://www.runoob.com/jsref/jsref-obj-regexp.html

https://www.runoob.com/jsref/jsref-regexp-wordchar-non.html

? 禁用 正则 贪婪模式 ✅

    getTitleLength (value) {
        if(!value) {
            return 0;
        }
        // ? 禁用正则贪婪模式 ✅
        const names = value.trim().match(/(\[\W+?\])/g);
        // [节日][节日] => ["[节日][节日]"] 动态词包连续 bug ❌
        // [节日]abc[节日] ["[节日]", "[节日]"] 不连续 ok ✅
        console.log('names =', names);
        let len = 0;
        // 动态词包,长度 (中文两个字节)
        const rel_names = this.customWords.map(obj => obj.rel_name);
        for (const name of names) {
            if(rel_names.includes(name)) {
                const obj = this.customWords.filter(obj => obj.rel_name === name)[0];
                if (obj.id) {
                    len += obj.max_word_len;
                    value = value.replace(obj.rel_name, '');
                };
            }
        }
        // 剩余字符串,长度 (中文两个字节,英文一个字节)
        const bytes = Util.getByteLen(value);
        return len + Math.ceil(bytes / 2);
    },

error

image


    getTitleLength (value) {
        if(!value) {
            return 0;
        }
        const names = value.trim().match(/(\[\W+?\])/g);
        // console.log('names =', names);
        let len = 0;
        // 动态词包,长度 (中文两个字节)
        const rel_names = this.customWords.map(obj => obj.rel_name);
        for (const name of names) {
            if(rel_names.includes(name)) {
                const obj = this.customWords.filter(obj => obj.rel_name === name)[0];
                if (obj.id) {
                    len += obj.max_word_len;
                    value = value.replace(obj.rel_name, '');
                };
            }
        }
        // 剩余字符串,长度 (中文两个字节,英文一个字节)
        const bytes = Util.getByteLen(value);
        return len + Math.ceil(bytes / 2);
    },

solution



    getTitleLength (value) {
        if(!value) {
            return 0;
        }
        // fix regex macth return null bug ✅ 
        // 修复正则表达式匹配返回空错误
        const names = value.trim().match(/(\[\W+?\])/g) ?? [];
        // console.log('names =', names);
        let len = 0;
        // 动态词包,长度 (中文两个字节)
        const rel_names = this.customWords.map(obj => obj.rel_name);
        for (const name of names) {
            if(rel_names.includes(name)) {
                const obj = this.customWords.filter(obj => obj.rel_name === name)[0];
                if (obj.id) {
                    len += obj.max_word_len;
                    value = value.replace(obj.rel_name, '');
                };
            }
        }
        // 剩余字符串,长度 (中文两个字节,英文一个字节)
        const bytes = Util.getByteLen(value);
        return len + Math.ceil(bytes / 2);
    },

refs

https://zh.javascript.info/regexp-groups

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://www.runoob.com/jsref/jsref-obj-regexp.html



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-06-11 01:08  xgqfrms  阅读(239)  评论(4编辑  收藏  举报