[英]Get a substring from a string for a regular expression in JavaScript
I have a string of the following form: 我有以下形式的字符串:
data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data
It can be in different languages, but in any case I need to get a string which is between the characters ' ' That is, in the example above, I need to get the following string: 它可以使用不同的语言,但是无论如何我都需要获取字符''之间的字符串。也就是说,在上面的示例中,我需要获取以下字符串:
view-7631b26ea80b1b601c313b15cc4e2ab03faedf30
Can I do this using the method string.replace(regexp, str) ? 我可以使用string.replace(regexp,str)方法吗?
I've highlighted the desired line using the following regular expression: 我使用以下正则表达式突出显示了所需的行:
/'\b(.*)\b'/gm
Now, using the method string.replace
I need to delete everything except that... 现在,使用
string.replace
方法删除除...以外的所有内容...
Got any suggestions? 有什么建议吗?
Use match
method. 使用
match
方法。
var data = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
data = data.match(/'\b(.*)\b'/gm)
You have good solid anchor text in either side, so: 两侧都有良好的实心锚文本,因此:
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
Live Example: 现场示例:
var str = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data"; var match = /data-translate='([^']+)'/.exec(str); var substr = match && match[1]; document.body.innerHTML = "<pre>Got: [" + substr + "]</pre>";
But again, as I said in a comment, using a simple regular expression to extract information from HTML is usually doomed to fail . 但是,正如我在评论中所说,再次使用简单的正则表达式从HTML提取信息通常注定会失败 。 For instance, you probably don't want to match this:
例如,您可能不想匹配以下内容:
<p>The string is data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'</p>
...and yet, a simple regex solution will do exactly that. ...然而,一个简单的正则表达式解决方案将做到这一点。 To properly handle HTML, you must use a proper HTML parser.
要正确处理HTML,必须使用正确的HTML解析器。
您也可以尝试以下一种方法:
/\'([^\']+)\'/gm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.