How to preg_match with javascript?

I have made this javascript where I can strReplace:

$('#update').click(function() {
    $("#mif").contents().find("#strDiv").each(function() {
        var s = '<?=$str;?>',
        sel = document.getElementById("tags"),
        re;
        for (var i = 0, len = sel.options.length; i < len; i++) {
            if (sel.options[i].selected) {
                re = new RegExp(sel.options[i].text.replace("<", "<\\\\/?").replace(">", "[^>]*?>"), "gi");
                s = s.replace(re, "");

            }
        }
        $("#mif").contents().find("#strDiv").html(s);
    });
});

With this I can replace and update an $str inside an iFrame without having to refresh and it works like a charm…

But what if I want to preg_replace like I can in PHP like this:

$results = preg_match_all('/(<h1>)(.*)(<\\/h1>)/', $str, $arr); 

I have 2 input fields with id pregmatchFrom and pregmatchTo. Now I want to be able to do the exact same thing as with the strReplace, but dont’ know where to begin… Can somebody please help :wink:

Thanks in advance :wink:

Think I might have got a bit closer:

function demoRegex() {
    var re = new RegExp('(<h1>)(.*)(<\\/h1>)', "g");
    var txt = '<h1>Peter Skylight</h1><img height="211" alt="" width="300" src="pic1.jpg" /><br /><span style="font-size: x-small">Peter Skylight is one of the most famous writers in our time.';
    newtxt = txt.replace(re,'');
    alert("Here: "+ newtxt);
}

This actually works, but if I change the var txt to a php variable it wont work???

So $str looks like this:

$str = '<h1>Peter Skylight</h1><img height="211" alt="" width="300" src="pic1.jpg" /><br /><span style="font-size: x-small">Peter Skylight is one of the most famous writers in our time.';

And the javascript like this:

function demoRegex() {
    var re = new RegExp('(<h1>)(.*)(<\\/h1>)', "g");
    var txt = <?=$str;?>;
    newtxt = txt.replace(re,'');
    alert("Here: "+ newtxt);
}

I then get this error in chrome javascriptconsole:
Uncaught SyntaxError: Unexpected identifier

Any ideas where I go wrong?
Thanks in advance…

… and found the solution…

echo '<input type="hidden" value="'.htmlentities($str).'" name="thestring" id="thestring">';
function demoRegex(){
	var txt = document.newform.thestring.value;
	var re = new RegExp(document.newform.pfrom.value, "g");
	newtxt = txt.replace(re,'');
	$("#mif").contents().find("#strDiv").html(newtxt);
}

Thanks to my self :wink: