admin管理员组文章数量:1201403
I am trying replace carriage return (\r) and newline (\n) and more than one spaces (' ' ) with single space.
I used \W+ which helped to achieve this but, it's replacing special characters also with space. I want to change this only replace above characters.
Please help me with proper regular expression with replace method in javascript.
I am trying replace carriage return (\r) and newline (\n) and more than one spaces (' ' ) with single space.
I used \W+ which helped to achieve this but, it's replacing special characters also with space. I want to change this only replace above characters.
Please help me with proper regular expression with replace method in javascript.
Share Improve this question asked Jan 29, 2015 at 10:37 user970503user970503 1731 gold badge1 silver badge10 bronze badges 2 |3 Answers
Reset to default 8This will work: /\n|\s{2,}/g
var res = str.replace(/\n|\s{2,}/g, " ");
You can test it here: https://regex101.com/r/pQ8zU1/1
\s match any white space character [\r\n\t\f ]
You should use \s{2,}
for this.It is made for this task.
This simple one should suit your needs: /[\r\n ]{2,}/g
. Replace by a space.
本文标签:
版权声明:本文标题:regex - Match of carriage return, line feed and multiple space in javascript regular expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738580060a2101109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
\r
AND\n
AND more than one space, or\r
OR\n
OR more than one space, because your question asks for the first but anubhava has provided the regex for the second. – Andy Commented Jan 29, 2015 at 10:42