[英]Javascript Match on parentheses inside the string
How do I do a .match on a string that has parentheses in the string? 如何对字符串中带圆括号的字符串执行.match操作? String1.match("How do I match this (MATCH ME)");
String1.match(“我如何匹配(MATCH ME)”);
None of the answers are getting me what I want. 没有一个答案能让我得到我想要的东西。 I'm probably just doing it wrong.
我可能只是做错了。 I tried to make my question basic and I think doing that asked my question wrong.
我试图让我的问题变得基本,我认为这样做会让我的问题出错。 This is the statement I am tring to fix:
这是我要修复的声明:
$('[id$=txtEntry3]').focus(function () {
if (!DropdownList.toString().match($('[id$=txtEntry2]').val()) || $('[id$=txtEntry2]').val().length < 5) {
$('[id$=txtEntry2]').select();
ErrorMessageIn("The Ingredient number you entered is not valid.");
return;
}
ErrorMessageOut();
});
This works correctly the problem I am running into is when it tries to match a entry from "txtEntry2" that has "()" in it. 这正确地解决了我遇到的问题,当它试图匹配“txtEntry2”中包含“()”的条目时。
Well it's kinda broken but it works for what I need it to do. 嗯,它有点破碎,但它适用于我需要它做的事情。 This is what I did to fix my problem:
这就是我为解决问题所做的工作:
$('[id$=txtEntry3]').focus(function () {
if (!StripParentheses(DropdownList).match(StripParentheses($('[id$=txtEntry2]').val())) || $('[id$=txtEntry2]').val().length < 5) {
$('[id$=txtEntry2]').select();
if (!$('[id$=txtEntry2]').val() == "") {
ErrorMessageIn("The Ingredient number you entered is not valid.");
}
return;
}
ErrorMessageOut();
});
function StripParentheses(String){
x = String.toString().replace(/\(/g, '');
x = x.toString().replace(/\)/g, '');
return x;
}
var str = "How do I match this (MATCH ME)";
str.match(/\((.*?)\)/);
to get all occurences in eg ".. (match me) .. (match me too) .." add the g
regexp flag 得到所有出现在例如“..(匹配我)..(也匹配我)..”添加
g
regexp标志
string.match(/\((.*?)\)/g)
this as also an advantage, that you get only list of all occurences. 这也是一个优点,你只得到所有出现的列表。 without this flag, the result will include a whole regexp pattern match (as the first item of resulting array)
没有这个标志,结果将包括一个完整的正则表达式模式匹配(作为结果数组的第一项)
If you are interested in the part of the string between parenthesis, then you can use /\\(([^\\)]*)\\)/
; 如果你对括号内的字符串部分感兴趣,那么你可以使用
/\\(([^\\)]*)\\)/
; if you just need to get the full string, then you can you can use /\\([^\\)]*\\)/
. 如果你只需要获取完整的字符串,那么你可以使用
/\\([^\\)]*\\)/
。
使用转义字符 - 请参阅此处: http : //www.javascriptkit.com/jsref/regexp.shtml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.