Reputation: 3145
I need a javascript regex that can distinguish between PHP tags in HTML tags and PHP tags outside of HTML tags.
e.g.
<input type="text" <? print '1'; ?> value="<? print '2'; ?>">
<? print '3';?>
So I need a regex to pull out:
<? print '1'; ?> and <? print '2'; ?>
And another regex to pull out:
<? print '3';?>
At the moment I have this regex which pulls out all PHP tags regardless of where they are:
/\n?<\?(php)?(\s|[^\s])*?\?>\n?/ig
Upvotes: 0
Views: 329
Reputation: 7191
This is a very complex thing to do, and I very much doubt it can be solved by regular expressions. This does depend to some degree on how complex the PHP that you want to extract is, but there are many cases to consider:
<?=max($a, $b);?>
<? echo max($a, $b); ?>
<?php echo ($a > $b) ? 'yes' : 'no'; ?>
<div><p><?php echo '</p>'; ?></div>
Why do you need to do this with JavaScript and regular expressions?
Upvotes: 1
Reputation: 3257
Do not use regex for HTML or XML tags. Instead, use parsing methods from XmlDocument
and XmlElement
and XmlAttribute
.
Upvotes: -1
Reputation: 75794
A). In a normal browser context Javascript won't be able to "see" the PHP at all. Where did you expect the document to be read from?
B). Regex is not a suitable tool for parsing HTML which is not a regular grammar. You have to use an XML/HTML parser.
Upvotes: 2