问题描述:

I'm very new to these, but have a regular expression/replace function:

string.replace(/\s{10,}/gi, ' ');

Because I have a text string that is out of control with a combination of white-spaces, tab spaces, and line-breaks. The problem I am facing is that the above expression handles too much. I have tried dialing it back to \s{1,} to alleviate this, but it reduces even line-breaks and tab-spaces down to a single space. I would like to handle those separately with different rules.

It seems like this rule is overriding anything I try to handle the other spacing types.

网友答案:

You can use an actual space character to match spaces only:

var spaces = / /g; // a valid regex

For other types consider these:

/\t\r\n/ // other space characters
网友答案:

If you want to deal only with white spaces, try it:

string.replace('[ ]+', ' ');
相关阅读:
Top