javascript javascript正则表达式来计算空白字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6867519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:08:54  来源:igfitidea点击:

javascript regex to count whitespace characters

javascriptregex

提问by hvgotcodes

Can I use a javascript regex to count the number of whitespace characters before the first text character in the string? I only care if there are 0, 1, and 2+.

我可以使用 javascript 正则表达式来计算字符串中第一个文本字符之前的空白字符数吗?我只关心是否有 0、1 和 2+。

My current working solution is to have three regexes and just use match to determine the 0,1, or 2+ category(separate pattern for each), but Im looking for a more elegant solution.

我目前的工作解决方案是拥有三个正则表达式,并仅使用匹配来确定 0,1 或 2+ 类别(每个类别的单独模式),但我正在寻找更优雅的解决方案。

Is it even possible to count patterns with regex? I could use a non-greedy grouping and count the length I guess....

甚至可以用正则表达式计算模式吗?我可以使用非贪婪分组并计算我猜的长度....

采纳答案by SLaks

"     a".match(/^\s{0,2}/)[0].length

This regex matches between 0 and 2 whitespace characters at the beginning of a string.

此正则表达式匹配字符串开头的 0 到 2 个空白字符。

回答by Enki

You can just do :

你可以这样做:

"   aaa".replace(/^(\s*).*$/,"").length

回答by jfriend00

I'm not sure I'd use a regex in real life for this job, but you can match them in a capture and the see their length:

我不确定我会在现实生活中使用正则表达式来完成这项工作,但是您可以在捕获中匹配它们并查看它们的长度:

function countLeadingSpaces(str) {
    return(str.match(/^(\s*)/)[1].length;
}

A non regex way designed for speed (pretty much anything is fast compared to a regex):

专为速度设计的非正则表达式方式(与正则表达式相比,几乎任何东西都快):

function countLeadingSpaces2(str) {
    for (var i = 0; i < str.length; i++) {
        if (str[i] != " " && str[i] != "\t") {
            return(i);
        }
    }
    return(str.length);
}

回答by kennebec

You could find the index of the first non-space character in the string, which is the same as the number of leading white space characters.

您可以找到字符串中第一个非空格字符的索引,该索引与前导空格字符的数量相同。

t.search(/\S/);

If you insist you can limit the return to 0, 1 or 2 with Math.min(t.search(/\S/), 2);

如果您坚持,您可以将返回值限制为 0、1 或 2 Math.min(t.search(/\S/), 2);

If there are nonon-space characters the return will be -1....

如果没有非空格字符,则返回值为 -1....

回答by paxdiablo

Why notjust use a capture group and check the length:

为什么使用捕获组并检查长度:

<html>
    <head>
    </head>
    <body>
        <script language="javascript">
            var myStr = "  hello";
            var myRe = /^(\s*)(.*)$/;
            var match = myRe.exec(myStr);
            alert(match[1].length);  // gives 2
            alert(match[2]);         // gives "hello"
        </script>
    </body>
</html

This can be followed by code that acts on your three cases, a length of 0, 1 or otherwise, and acts on the rest of the string.

这之后可以是作用于您的三种情况的代码,长度为 0、1 或其他,并作用于字符串的其余部分。

You should treat regexes as the toolthey are, not as an entire toolbox.

您应该将正则表达式视为工具,而不是整个工具箱。

回答by aroth

This should do the job:

这应该可以完成这项工作:

string.split(/[^ \t\r\n]/)[0].length;

Example: http://jsfiddle.net/Nc3SS/1/

示例:http: //jsfiddle.net/Nc3SS/1/