[英]RegEx - Get All Characters After A Specific Character (#)
使用下面的字符串,如何编写正则表达式以删除或单独保存仅出现在#
符号后面的字符。
var category = "http://schemas.google.com/g/2005#work"
You can use this regex: 您可以使用此正则表达式:
var category = "http://schemas.google.com/g/2005#work";
var hashval = category.replace(/^[^#]*#([\s\S]*)$/, '$1');
//=> work
OR better using String#match: 或者使用String#match更好:
var hashval = category.match(/#([\s\S]*)$/)[1];
//=> work
The nice way is to let the browser do the heavy lifting. 好的方法是让浏览器完成繁重的工作。
var category = "http://schemas.google.com/g/2005#work",
link = document.createElement('a'),
hash;
link.href = category;
hash = link.hash;
This has your browser create an a
element and set the the href
property of that element to be category
. 这使您的浏览器创建
a
元素并将a
元素的href
属性设置为category
。 The browser then parses the URL. 浏览器然后解析URL。 The link's
hash
property is the bit after the #
in the URL. 链接的
hash
属性是URL中#
后面的位。
I answered a similar question some time ago, with a more worked-out version of this code. 不久前,我回答了一个类似的问题 ,并用了更完善的代码。
i think another way, is using a Capture Group : 我认为另一种方式是使用捕获组 :
\#(.*)
In this way, it is possible to create other different groups to capture different elements. 这样,可以创建其他不同的组来捕获不同的元素。
Hope this helps = D 希望这可以帮助= D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.