[英]find a string with regex and javascript
I am trying to match the string 6c81748b9239e96e
it's random each time. 我试图匹配字符串
6c81748b9239e96e
每次都是随机的。 Using the following code below. 使用以下代码。 My problem is that it matches the entire string and I only need the random string that contains the letters and numbers.
我的问题是它匹配整个字符串,我只需要包含字母和数字的随机字符串。
String 串
<a href="playgame.aspx?gid=4&tag=6c81748b9239e96e">Play</a>
javascript regex javascript正则表达式
string.match(/\&tag\=[A-Za-z0-9]+\"\>/i);
You could use Regular Expression Groups to match, and later access the pattern you are after. 您可以使用正则表达式组进行匹配,然后再访问您所使用的模式。 The regex you will need to use is like so:
/\\&tag\\=([A-Za-z0-9]+)\\"\\>/i
. The round brackets ( (
and )
) will denote the group you want to capture. You can then access the capture group as shown here . 你需要使用的正则表达式如下:
/\\&tag\\=([A-Za-z0-9]+)\\"\\>/i
。圆括号( (
和)
)将表示你想要的组捕获。然后,您可以访问捕获组如图所示这里 。
EDIT: Upon closer inspection it seems that you might be using an incorrect regular expression. 编辑:仔细检查后,您可能正在使用不正确的正则表达式。 I am not really used to Javascript regexes but it seems that you are escaping the
&
and =
and >
, which is not required. 我并不习惯Javascript正则表达式,但似乎你正在逃避
&
和=
和>
,这不是必需的。 Try this instead: /&tag=([A-Za-z0-9]+)\\">/i
. 请尝试这样:
/&tag=([A-Za-z0-9]+)\\">/i
。
Here is my suggestion: 这是我的建议:
Add the snippet provided by @Artem Barger to your code: https://stackoverflow.com/a/901144/851498 You need to slightly modify it, though (adding the str
argument): 将@Artem Barger提供的代码段添加到您的代码中: https ://stackoverflow.com/a/901144/851498您需要稍微修改它(添加
str
参数):
function getParameterByName( name, str ) { name = name.replace(/[\\[]/, "\\\\\\[").replace(/[\\]]/, "\\\\\\]"); var regexS = "[\\\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec( str ); if(results == null) return ""; else return decodeURIComponent(results[1].replace(/\\+/g, " ")); }
Use it this way: 用这种方式:
var str = getParameterByName( 'tag', string );
Jsfiddle demo: http://jsfiddle.net/Ralt/u9MAv/ Jsfiddle演示: http : //jsfiddle.net/Ralt/u9MAv/
var myregexp = /(&tag=)([A-Za-z0-9]+)\b/img;
var match = myregexp.exec(subject);
while (match != null) {
for (var i = 0; i < match.length; i++) {
// matched text: match[i]
}
match = myregexp.exec(subject);
}
Your regex 你的正则表达式
&tag=([A-Za-z\d]+)"
It's simplified (you escaped too much) and parenthesis were added to put the thing you want in group 1 它被简化了(你逃脱了太多)并且添加了括号以将你想要的东西放在第1组中
In javascript this becomes 在javascript中,这变成了
var myregexp = /&tag=([A-Za-z\d]+)"/;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}
Explanation 说明
Match the characters “&tag=” literally «&tag=»
Match the regular expression below and capture its match into backreference number 1 «([A-Za-z\d]+)»
Match a single character present in the list below «[A-Za-z\d]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A character in the range between “A” and “Z” «A-Z»
A character in the range between “a” and “z” «a-z»
A single digit 0..9 «\d»
Match the character “"” literally «"»
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.