Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts

    RegEx that works in PHP but not Javascript

    I have this:
    PHP Code:
        $teststring '<span style="font-weight: bold;">test</span></span>';
        echo 
    preg_replace('/<span style="font-weight: bold;">(.*)(?<!<\/span>)<\/span>/''<strong>$1</strong>'$teststring);
        
    // result: <strong>test</strong></span> 
    but in Javascript I get an error form this:
    Code:
    var text =  '<span style="font-weight: bold;">test</span></span>';
    text = text.replace(/<span style="font-weight: bold;">(.*)(?<!<\/span>)<\/span>/g, '<strong>$1</strong>');
    /* result: invalid quantifier ? */
    What am I missing? Or, what's another way to do this? I just want to make sure the expression matches the first closing span like in the PHP example, by default it matches the last.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Maybe this will help.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function checkme(form){
    	if(!(/^<span style="font-weight: bold;">$/.test(form.thespan.value))){
    		form.thespan.focus();
    		alert('does not match');
       		return false;
    	}
    	else {
    		alert('it does match');
    		return false;
    	}
    }
    </script>
    </head>
    
    <body>
    <form onsubmit="return checkme(this)">
    <input type="text" name="thespan">
    <input type="submit" value="submit">
    </form>
    </body>
    </html>

  • #3
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    I think that just matches <span style="font-weight: bold;"> at the end of a string, while I need to match <span style="font-weight:bold;">(.*)(the first </span> after the opening tag) anywhere in the string.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, this will only works in newer browsers, but try using a non-greedy match.)
    Code:
    re=/<span\sstyle="font-weight:\sbold;">.*?<\/span>/
    JavaScript regex can't exclude strings from a pattern, I'm afraid, so this is probably the only way to do it unless you want to just search for the next '<'.
    Last edited by liorean; 12-17-2005 at 09:39 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by liorean
    Well, this will only works in newer browsers, but try using a non-greedy match.)
    Code:
    re=/<span\sstyle="font-weight: bold;">.*?<\/span>/
    JavaScript regex can't exclude strings from a pattern, I'm afraid, so this is probably the only way to do it unless you want to just search for the next '<'.
    Thanks!

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The piece it doesn't like is:

    (?<!<\/span>)

    The first < is messing it up as Javascript doesn't use that notation. If you want to use a lookahead group with NOT </span> you need to specify the following Javascript

    (?!<\/span>)

    of course making the prior group reluctant rather than greedy is a more familiar way to do it.

    (.*?)
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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