C
Christoph Boget
I'm trying to get a regular expression to work in JS. It appears to be
working everywhere else I'm testing it (an app called Regex Coach and php)
but I can't seem to get it to work in JS. What the regex is supposed to do
is:
<p></p>
OR
<br>
OR
<br/>
OR
<br />
OR
<p></p>
OR
<p><br></p>
OR
<p><br/></p>
OR
<p><br /></p>
OR
<p>[multiple of any of the above br's]</p>
taking into account any number of interlaced spaces. The regex I came up
with is:
^\s*<p\s*>(?:<br\s*\/?>|\s)*<\/p>|(?:<br\s*\/?>|\s)*\s*$
which, as I said, seems to work elsewhere. However, no matter how I try to
use it in JS, using the test() method against it returns true against text
when it should be returning false. The things I've tried are as follows:
var re = /^\s*<p\s*>(?:<br\s*\/?>|\s)*<\/p>|(?:<br\s*\/?>|\s)*\s*$/;
re.test( MyStringValue );
var re = /^\s*<p\s*>(?:<br\s*\/?>|\s)*<\/p>|(?:<br\s*\/?>|\s)*\s*$/gim;
re.test( MyStringValue );
var re = new RegExp(
'^\<p\s*\>[\<br\s*\/{0,1}\>|\s]*\<\/p\>|[\<br\s*\/{0,1}\>|\s]*$' );
re.test( MyStringValue );
var re = new RegExp(
'^\<p\s*\>[\<br\s*\/{0,1}\>|\s]*\<\/p\>|[\<br\s*\/{0,1}\>|\s]*$', 'gim' );
re.test( MyStringValue );
But it's failing the test (returning true) on things like
<p>
<br>
<br/>lskadfakjsdf;lja <br>
</p>
for example. What gives? Am I doing something wrong? It seems like it's
working elsewhere, just not in JS...
thnx,
Christoph
working everywhere else I'm testing it (an app called Regex Coach and php)
but I can't seem to get it to work in JS. What the regex is supposed to do
is:
<p></p>
OR
<br>
OR
<br/>
OR
<br />
OR
<p></p>
OR
<p><br></p>
OR
<p><br/></p>
OR
<p><br /></p>
OR
<p>[multiple of any of the above br's]</p>
taking into account any number of interlaced spaces. The regex I came up
with is:
^\s*<p\s*>(?:<br\s*\/?>|\s)*<\/p>|(?:<br\s*\/?>|\s)*\s*$
which, as I said, seems to work elsewhere. However, no matter how I try to
use it in JS, using the test() method against it returns true against text
when it should be returning false. The things I've tried are as follows:
var re = /^\s*<p\s*>(?:<br\s*\/?>|\s)*<\/p>|(?:<br\s*\/?>|\s)*\s*$/;
re.test( MyStringValue );
var re = /^\s*<p\s*>(?:<br\s*\/?>|\s)*<\/p>|(?:<br\s*\/?>|\s)*\s*$/gim;
re.test( MyStringValue );
var re = new RegExp(
'^\<p\s*\>[\<br\s*\/{0,1}\>|\s]*\<\/p\>|[\<br\s*\/{0,1}\>|\s]*$' );
re.test( MyStringValue );
var re = new RegExp(
'^\<p\s*\>[\<br\s*\/{0,1}\>|\s]*\<\/p\>|[\<br\s*\/{0,1}\>|\s]*$', 'gim' );
re.test( MyStringValue );
But it's failing the test (returning true) on things like
<p>
<br>
<br/>lskadfakjsdf;lja <br>
</p>
for example. What gives? Am I doing something wrong? It seems like it's
working elsewhere, just not in JS...
thnx,
Christoph