SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to preg_match with javascript?

    I have made this javascript where I can strReplace:
    Code JavaScript:
    $('#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:
    PHP Code:
    $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 ;-)

    Thanks in advance ;-)

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think I might have got a bit closer:

    Code JavaScript:
    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:
    PHP Code:
    $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:
    Code JavaScript:
    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...

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ... and found the solution...
    PHP Code:
    echo '<input type="hidden" value="'.htmlentities($str).'" name="thestring" id="thestring">'
    Code JavaScript:
    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 ;-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •