www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problems porting python regex to JS

  1. #1
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    88

    Problems porting python regex to JS

    Hi, I tried to port some python regex to JS, but the results were unexected
    Code:
    text = "[start 0.0]one one[end 5.25]this should not display[start 9]two two.[end 18.5][start 30]three three[end 50]";
    document.write(text + "<br/ ><br/ >");
    
    // define regex
    starts = /\[start\s([\d\.]+)\]/g;
    document.write(text.match(starts) + "<br/ ><br/ >");
    
    words = /\[start\s[\d\.]+\](.*?)\[end\s[\d\.]+\]/g;
    document.write(text.match(words) + "<br/ ><br/ >");
    
    ends = /\[end\s([\d\.]+)\]/g;
    document.write(text.match(ends) + "<br/ ><br/ >");
    Which returns...
    Code:
    [start 0.0]one one[end 5.25]this should not display[start 9]two two.[end 18.5][start 30]three three[end 50]
    
    [start 0.0],[start 9],[start 30]
    
    [start 0.0]one one[end 5.25],[start 9]two two.[end 18.5],[start 30]three three[end 50]
    
    [end 5.25],[end 18.5],[end 50]
    Here is the python, which returns what I expected...
    Code:
    s = '\[start\s([\d\.]+)\]'
    start = re.findall(s, text)
    
    # Start and end times for each word
    e = '\[end\s([\d\.]+)\]'
    end = re.findall(e, text)
    
    # this will get you the word you need to display.
    w = '\[start\s[\d\.]+\](.*?)\[end\s[\d\.]+\]'
    words = re.findall(w, text)
    returns...
    Code:
    >>> start
    ['3.3', '8', '16']
    >>> end
    ['6', '13', '20']
    >>> words
    ['one one', 'two two.', 'three three']
    Anyone know why this is?
    Thanks.

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object (w3cschools.com). Then you should write something like this :
    Code:
    text = "[start 0.0]one one[end 5.25]this should not display[start 9]two two.[end 18.5][start 30]three three[end 50]";
    document.write(text + "<br/ ><br/ >");
    
    startReg= /\[start\s([\d\.]+)\]/g;
    starts=text.match(startReg);
    for (i=0;i<starts.length;i++)document.write(starts[i]+",  ");
    document.write("<br/ ><br/ >");
    It would still be advisable to remove the brackets and the word start... Then the replace method with a function will be more effective, with only one regular expression (do not use document write, which is only valid for an opening document).
    Code:
    <body>
    <div id="pge"></div>
    <script type="text/javascript">
    text = "[start 0.0]one one[end 5.25]this should not display[start 9]two two.[end 18.5][start 30]three three[end 50]";
    // The document is existing at the end of the body
    document.getElementById('pge').innerHTML=text + "<br>";
    
    // We use a replace method only to define new variables
    var start=end=word=''; 
    text.replace(/\[start\s([\d\.]+)\]([^\[]+)\[end\s([\d\.]+)\]/g,
    		function(allMatch,firstSubPat,secondSubPat,thirdSubPat){
    		   start+=', '+firstSubPat;
    		   word+=', '+secondSubPat;
    		   end+=', '+thirdSubPat;
    		});
    document.getElementById('pge').innerHTML+='start : '+start.substr(2)+'<br>&nbsp;end : '+end.substr(2)+'<br>word : '+word.substr(2);		
    </script>
    </body>
    Obviously start, word and end could be arrays...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles