常用正则表达式

1.验证url

function validateUrl(url) {
  const pattern = new RegExp(
        "^((https|http|ftp|rtsp|mms)?://)" +
        "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + //ftp的user@
        "(([0-9]{1,3}\.){3}[0-9]{1,3}" + // IP形式的URL- 199.194.52.184
        "|" + // 允许IP和DOMAIN(域名)
        "([0-9a-z_!~*'()-]+\.)*" + // 域名- www.
        "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." + // 二级域名
        "[a-z]{2,6})" + // first level domain- .com or .museum
        "(:[0-9]{1,4})?" + // 端口- :80
        "((/?)|" + // a slash isn't required if there is no file name
          "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"
      );
  return pattern.test(url);
}

function validateUrl(url) {
   const urlRegex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i;
  return urlRegex .test(url);
}

2.HTML标签去除

   filterHTML(htmlStr) {
      return htmlStr.replace(/<[^>]+>/g, "");
    },

你可能感兴趣的:(前端开发,前端,javascript,正则表达式)