[英]How to use variable to dynamically build regular expression object
我想即時建立正則表達式,例如:
function buildReges(regex_string) {
// here I want to use the string format to build a regex
// like / + regex_string +/
}
我不知道該怎么辦? 還是有辦法將字符串轉換為正則表達式對象?
使用RegExp
構造函數:
function buildReges(regex_string) {
// here I want to use the string format to build a regex
var re = new RegExp(regex_string);
}
如果要使用標志,請使用第二個參數:
new RegExp(regex_string, 'gi');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.