下面的代码工作正常,如果我正在代码中对模式进行硬编码,如果我从数据库中获取了该模式,那么它将无法正常工作。谁能告诉我我做错了什么
//This line works
string regularExpressionPattern = @"\[(.*?)\]";;
//But this line when im fetching this from database as i have stored the pattern in it, it doesnt work. I'm assigning it the same value as above but this time from database
string regularExpressionPattern = regExPattern.ToString();
string inputText = p.Text.ToString();
Regex re = new Regex(regularExpressionPattern);
foreach (Match m in re.Matches(inputText))
{
Response.Write("Match Found");
}
答案 0 :(得分:0)
如果将模式存储为字符串,则可能需要将其恢复为正确的模式样式。这取决于您如何在db中存储该模式。
function getRegFromString(string){
var a = string.split("/");
modifiers= a.pop(); a.shift();
pattern = a.join("/");
return new RegExp(pattern, modifiers);
}
getRegFromString(regExPattern);
在此处查看更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString