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

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

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:

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

Need Your Help

MVVM Madness: Commands

c# .net wpf design-patterns mvvm

I like MVVM. I don't love it, but like it. Most of it makes sense. But, I keep reading articles that encourage you to write a lot of code so that you can write XAML and don't have to write any code...

How to deal with special characters in URLs inside XML

php javascript xml url special-characters

I have an XML element that has a url as one of it's children, for example:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.