微信小程序 正则字符串转为正则对象

场景:
服务器返回的一个正则表达式是一个字符串类型的, 直接拿去配置正则是不可以的, 需要转为正则对象, 然后去验证, 网页可以使用evel()对象, 但是微信小程序就不行,
方式如下:

        let regex = regExpString.match(/^\/([\S\s]+)\/([gim]{0,3})$/);
        regex = regex || [, regex, ""];      // If the previous match is null,
                                             // treat the string as a slash-less RegEx
        regex = new RegExp(regex[1], regex[2]);
        console.log('转换后的正则表达式', regex)

原文地址: https://www.oomake.com/question/2225716

posted @ 2023-03-28 16:53  山村码农  阅读(99)  评论(0编辑  收藏  举报