0
replace(/(?<=<\?php)[\w\W]*(?=\?>)/g, " ");

I want to replace the words between <?php and ?>, but it doesn't work in JavaScript. I have heard that JavaScript doesn't support regular expressions. How else can I solve this problem?

2
  • 8
    Whoever told you that JavaScript doesn't support regular expressions is sorely misinformed. Commented Jun 24, 2012 at 2:36
  • 5
    Why do you have text between <?php ?> that is available to JavaScript? Hopefully it has not been sent down to the client. Commented Jun 24, 2012 at 2:37

2 Answers 2

6

JavaScript doesn't support lookbehinds (the (?<= part) in regular expressions. You can just replace it with <?php ?> and drop the assertions, though:

replace(/<\?php.+?\?>/g, "<?php ?>");
Sign up to request clarification or add additional context in comments.

1 Comment

@final. If it helped you, don't forget to click on the check sign on the left of the answer.
1

Look behinds and look aheads do not work instead rewrite your regex.

replace(/(<\?php)([\w\W]*)(\?>)/g, "$1$3")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.