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

JS uses regular expressions to find the length of the longest continuous substring

小云云
Release: 2018-01-02 15:42:01
Original
1983 people have browsed it

本文主要介绍了js 正则找出最长连续子串长度的实现代码,需要的朋友可以参考下,希望能帮助到大家。

废话不多说了,直接给大家贴代码了,具体代码如下所示:

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之正则表达式捕获组与非捕获组的详解

PHP正则表达式的基础及简单实例

The above is the detailed content of JS uses regular expressions to find the length of the longest continuous substring. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!