search

Home  >  Q&A  >  body text

javascript - Regular expression question and answer

var html = "<p><a href='http://www.cnblogs.com/rubylouvre/'>Ruby Louvre</a>by <em>司徒正美</em></p>";
var text = html.replace(/<(?:.|\s)*?>/g, "");
console.log(text);//VM247:3 Ruby Louvreby 司徒正美

Please explain this regular expression to me. I really can’t understand π-π. . .
Thank you

三叔三叔2757 days ago963

reply all(2)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-26 10:52:05

    /    # 开始符
    <    # 就是<这个字符
    (    # 括号表示捕获, 意思是能在 正则的group函数中, 获取相应的值
    ?:    # 这个又取消括号的捕获功能, 就是上一行的说明无效了.
    .    # 单个字符
    |    # 逻辑或的意思, 范围为括号中
    \s    # 空白
    )    # 一对括号
    *    # 一个或多个, 这儿表示 一个或多个 括号中的内容
    ?    # 非贪婪, 表示 前面()* 这个组能少就少
    >    # 字符>
    /g    # 表示全局搜索, 就是 match_all 的意思.

    What you have here is the replacement function, which replaces <xxxx> with empty spaces

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-26 10:52:05

    You can go to https://regex101.com/ and try it yourself. You can enter expressions on the left side. Enter the matched content of the test below. There is a detailed explanation on the right.
    In addition, the |s in this regular expression is wasteful. You only need to write /<(?:.)*?>/g. If there is no need to operate on the capturing group, there is no need for non-capturing group. necessary. It can be simplified to /<.*?>/g

    reply
    0
  • Cancelreply