我该如何使用?在Regex中使用Javascript [英] How Do I Use ? In Regex In Javascript
本文介绍了我该如何使用?在Regex中使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi.
I have string "? string:45dea2c1-70a5-4d79-8e67-03859965d58d ?" like this. I would like to get guid. How can i do
<pre lang="Javascript">
var pattern = \? string:([a-zA-Z0-9_]) \?
var value = "? string:45dea2c1-70a5-4d79-8e67-03859965d58d ?"
var res = value.match(pattern)
</pre>
doesnot work
推荐答案
A正则表达式应该在正斜杠内,用反斜杠来转义特殊字符,如问号:
A regex should be inside forward slashes, with backslashes to escape special characters like the question marks:
var pattern = /\? string:([a-zA-Z0-9-]+) \?/;
我用连字符( - )替换了下划线(_)匹配GUID内部,并在末尾添加+以匹配至少一个字符。 value.match(pattern)
将返回一个包含完整字符串和GUID的数组。
I've replaced the underscore (_) with a hyphen (-) to match that inside the GUID, and added the "+" at the end to match at least one character. value.match(pattern)
will return an array containing the full string and the GUID.
这篇关于我该如何使用?在Regex中使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文