Home > Web Front-end > JS Tutorial > body text

Get the longest continuous substring through regularity

php中世界最好的语言
Release: 2018-04-16 16:51:26
Original
1548 people have browsed it

给大家带来通过正则取得最长连续子串,通过正则取得最长连续子串的注意事项有哪些,下面就是实战案例,一起来看一下。

function maxLenStr(str){
  var len = 0, max_len = 0;
  var reg = new RegExp("(.)\\1{1,}","g");
  var res = reg.exec(str);
  while(res != null){
    len = res[0].length;
    if(max_len < len){
      max_len = len;
    }
    res = reg.exec(str)
  }
  return max_len;
}
Copy after login

js使用正则查找子串

var str = '#param1#abcdef#param2#hjklllj#param3#7878'
var count = str.match(/param\d*/g)
console.log(count) // ["param1", "param2", "param3"]
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:



The above is the detailed content of Get the longest continuous substring through regularity. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template