简体   繁体   English

字符串中的特殊字符#

[英]Special Character # in a string

I have a string tmp which is as follows 我有一个字符串tmp,如下所示

  <a href="#" class="quickLinks" id="linkQL">Quick Links</a> 

I would like to be able to know that there is a # after href= .. I thought of doing it this way if (tmp.indexOf('#') == 8) but I am not able to , I think its because # is a special character 我希望能够知道有一个#after href = ..我想这样做(tmp.indexOf('#')== 8)但是我不能,我认为是因为#是一个特殊的角色

To test if # follows href, you can simply to this: 要测试#是否跟随href,你可以简单地这样做:

if (input.contains("href=\"#\""))

There is no need to involve regex, and the hash char # is not special in java, but the double quote chars need escaping. 没有必要涉及正则表达式,并且哈希字符#在java中并不特殊,但双引号字符需要转义。

If you want to check the href attribute contains a # , then use a HTML parser like jsoup 如果要检查href属性是否包含# ,则使用像jsoup这样的HTML解析器

Document doc = Jsoup.parse(html);
String href = doc.select("a").first().attr("href");
boolean isHash = href.equals("#");

If you really want to just check if the there is a # anywhere on the line after a href , you could use Regex - but I suspect that is not what you want. 如果你真的只想检查一下href之后是否有#在线上的任何地方,你可以使用正则表达式 - 但我怀疑这不是你想要的。

var tmp = '<a href="#" class="quickLinks" id="linkQL">Quick Links</a>';
tmp.indexOf('#');
这是返回9,这是预期的结果。

尝试正则表达式

boolean matches = s.matches(".+href\\s*=\\s*['\"]#['\"].+");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM