Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter   in textarea with regex?

btw: i know that using regex is not the best idea in the world...

For example i have such input variants:

<p>&nbsp; &nbsp; &nbsp;</p>

or

<p>&nbsp; &nbsp;</p>

or

<p>&nbsp;</p>

and i want to check my input like: all except <p> with &nbsp; in every amount of them (0, 1 or 50)...

i wrote such expression:

/[^<p>(\s*&nbsp;\s*)*<\/p>]/ig

and seems that it works, but!

for example i have such input:

<p>&nbsp; &nbsp;t&nbsp;</p>

or

<p>&nbsp; &nbsp;tttt&nbsp;tttt</p>

and it is thinking, that it is equal to my regex...

not a good idea...

what i do wrong in my regex? or maybe there are some better ways of solving this?

like image 991
brabertaser19 Avatar asked May 29 '15 11:05

brabertaser19


People also ask

How does filter work in Excel?

The FILTER function in Excel is used to filter a range of data based on the criteria that you specify. The function belongs to the category of Dynamic Arrays functions. The result is an array of values that automatically spills into a range of cells, starting from the cell where you enter a formula.

How do you use the filter function?

The FILTER function takes three arguments: array, include, and if_empty. Array is the range or array to filter. The include argument should consist of one or more logical tests. These tests should return TRUE or FALSE based on the evaluation of values from array.


1 Answers

Assuming you want to eliminate all <P>'s with only nbsp; (or more) inside them : then :

Assuming you have this

var a='a<p>&nbsp; &nbsp;&nbsp;</p>c<p>&nbsp; &nbsp;&nbsp;</p>d<p>&nbsp;aa &nbsp;&nbsp;</p>e';

And assuming the yellow part has to go : becuase it contains aa inside :

You'll be left with all except the problematic P with pure nbsps :

enter image description here

Then this code :

a=a.replace(/(<p>.*?<\/p>)/g, function(match, p1 ) {
  if (/^<p>(\s*&nbsp;\s*)*<\/p>$/ig.test(p1)) 
  return '';
  else return p1;
})

Will yield :

acd<p>&nbsp;aa &nbsp;&nbsp;</p>e

As you can see - the P tag wasn't removed because of aa

http://jsbin.com/cizidayeru/3/edit

like image 129
Royi Namir Avatar answered Oct 11 '22 18:10

Royi Namir