CodeMan03
CodeMan03

Reputation: 226

Regular Expression remove whitespace between letters

I messed up my html somehow and all of my html now looks like this with a whitespace between each character

< d i v  c l a s s = " c o l - x s - 1 2   c o l - s m - 6 " >                               
< d i v  c l a s s = " f o r m - g r o u p   c o l - x s - 1 2 "> 
< d i v  c l a s s = " r a d i o "> 
< l a b e l  c l a s s = " r a d i o - l a b e l  "> 

It is over 2000 lines of code. I need to go back looking like this

<div class = "col-xs-12 col-sm-6">                               
<div class = "form-group col-xs-12" > 
<div class = "radio" > 
<label class = "radio-label" > 

What regular expression can I use with notepad++ to fix this?

Upvotes: 1

Views: 111

Answers (2)

Steve Clanton
Steve Clanton

Reputation: 4314

Notepad++ doesn't support very advanced regex. Your best bet is to do a find and replace to replace two consecutive spaces with a special character that doesn't appear in your page, for example ~.

< d i v~c l a s s = " c o l - x s - 1 2~ c o l - s m - 6 " >~ 
< d i v~c l a s s = " f o r m - g r o u p~ c o l - x s - 1 2 "> 
< d i v~c l a s s = " r a d i o "> 
< l a b e l~c l a s s = " r a d i o - l a b e l~"> 

Now, you can replace all the spaces with the empty string to remove them:

<div~class="col-xs-12~col-sm-6">~
<div~class="form-group~col-xs-12">
<div~class="radio">
<label~class="radio-label~">

We have to replace the special character (~) with a space now, and we're done:

<div class="col-xs-12 col-sm-6"> 
<div class="form-group col-xs-12">
<div class="radio">
<label class="radio-label ">

Since you mention you have Visual Studio, this is actually easier to solve with Visual Studio if you have it, since you can use a more advanced regex: Find (\S)\s or (.)\s and replace with $1.

If you have the TextFX plugin installed, you can run fairly complex regular expressions in notepad++ through TextFX -> TextFX Quick -> Find/Replace. You can run (\S)\s or (.)\s as the expression to find and use \1 for the replace.

Upvotes: 2

Toto
Toto

Reputation: 91488

How about:

Find what: (?<=\S)\s(?=\S)
Replace with: NOTHING

This will replace every space \s that is between two non spaces \S.

(?<=\S) is a positive lookbehind
(?=\S) is a positive lookahead

Upvotes: 1

Related Questions