[英]Javascript regex: remove space(s) if not surrounded by a letter
I'm trying to clean some html text with javascript, there are white spaces included before and after some words (text is poorly formatted). 我正在尝试使用javascript清理一些html文本,在某些单词前后(文本格式不正确)包含空格。
Currently I have this regex: 目前我有这个正则表达式:
$("#" + target + " *").replaceText(/([\S][\u05B0-\u05C4]*)/gi, '<span class="marked">$1<\/span>');
This will capture all the non white-space characters and wrap them in a span element, but will not capture spaces between words (I need the span). 这将捕获所有非空格字符并将它们包装在span元素中,但不会捕获单词之间的空格(我需要span)。
How would you solve this? 您将如何解决?
This will match multiple repeated 这将匹配多个重复
(spaces) and replace them with a single space:
(空格)并将其替换为一个空格:
'Quick Brown Fox'.replace(/[ ]+/g, ' '); //returns 'Quick Brown Fox'
This will match multiple repeated \\n\\r\\t
(whitespace symbols - spaces, tabs, new-lines and line-breaks) and replace them with a single space: 这将匹配多个重复的
\\n\\r\\t
(空格符号-空格,制表符,换行符和换行符),并将它们替换为一个空格:
'Quick Brown Fox'.replace(/\s+/g, ' '); //returns 'Quick Brown Fox'
I don't understand your explanation of what you're trying to achieve with span
wraparounds, but you can do whatever you want with the output from above. 我不明白您对
span
环绕所要实现的目标的解释,但是您可以根据上面的输出执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.