简体   繁体   English

Javascript正则表达式:如果没有字母包围,请删除空格

[英]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'

Fiddled 拨弄

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.

相关问题 javascript正则表达式删除空格和字母 - javascript regex remove white space and a letter Javascript:如何编写正则表达式来检查符号是否被所有出现的空格包围 - Javascript : How to write a regex to check if a symbol is surrounded by space for all occurences Javascript RegEx:捕获-A,但不捕获AA。 其中A是集合[A-Za-z]中的任何字母。 破折号后跟字母通行证,破折号被字母包围 - Javascript RegEx: capture -A but not A-A. where A is any letter in the set [A-Za-z]. dash followed by letter passes, dash surrounded by letters doesnt 正则表达匹配:被空间或起点所包围,但不匹配 - Regex Matching: Surrounded by space or start of line, but not matched JavaScript 正则表达式 - 在空格后的字母之间添加 ~ 符号 - JavaScript Regex - add ~ symbol between lettes except letter after space javascript正则表达式最里面的括号不要用引号引起来 - javascript regex innermost parentheses not surrounded by quotes JavaScript正则表达式,用于由特定字符串包围的数字和空格 - JavaScript Regex for numbers and spaces surrounded by specific strings Javascript 和正则表达式:删除字符串中最后一个单词后的空格 - Javascript and regex: remove space after the last word in a string 正则表达式删除尾随空间 - RegEx to Remove Trailing Space 替换在Javascript正则表达式中不被单引号引起来的双引号 - Replace double quotes not surrounded by single quotes in Javascript regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM