9

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+.

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.

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

1
  • Does 2+ mean two or more? Is it your intent to produce a count for each kind of string? Commented Jul 29, 2011 at 1:07

6 Answers 6

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

Why not theString.match(/^\s+/)[0].length -- the OP means, I think 2+ to mean two or more, although it was strangely worded.
@Ray: It sounds like there are only three categories.
correct, only three categories: 0, 1, or 2+. I also need to get everything after the leading whitespaces, like a java trim.
to java trim str.replace(/^\s*(.*?)\s*$/,"$1")
@SLaks Oh my bad. I meant \s* by the way. Good answer!!
4

You can just do :

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

1 Comment

Doesn't seem to work when there are zero initial spaces - it returns the length of the whole word.
3

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);
}

5 Comments

Why not use \s* and get rid of the "matches" check altogether?
/^(\s*)[^ \t]/ of an empty string returns null which means you can't directly dereference [1].length on a match using it without some sort of check. I'm not suggesting my method is the most compact, but I was trying to be safe for all edge cases.
aah, I see - but I think it's returning null since it cannot find the [ \t] bit. Since the regexes are greedy anyway, you can just use ^(\s*) on its own.
Right you are. I updated the answer. This ends up with something similar to what other folks have.
But succinct and reusable, so +1 for that :-)
2

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);

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

Comments

0

This should do the job:

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

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

Comments

0

Why not just 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.