1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

regular expression

Discussion in 'HTML, Graphics & Programming' started by toastyman, 17 Jun 2009.

  1. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    Hi all,

    I've got a simple regular expression in JavaScript but I can't get it working. I'm simply trying to search a string for words that match a certain format, and then replace them with a bit of code.

    For example, it'd search this string..
    Code:
    The :fox jumped over the :gate into the field
    
    and replace it with
    Code:
    The "+$('fox').val()+" jumped over the "+$('gate').val()+" into the field
    
    All I have for the regular expression so far is ":[a-zA-Z] ", and that doesn't seem to work. If anyone could give me some pointers it'd be appreciated..
     
  2. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    You need to quantify your character class with a +, which tells it to take as many matches for the preceding group as possible. You can also omit the capitals in your character class by putting the i modifier on the end of the expression, which tells it to ignore case. Try this:

    Code:
    /:[a-z]+/i
     
  3. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    Fantastic, thanks! I've just tried using that reg expr and it does seem to work, but it only seems to replace the first match...

    Code:
    var str = 'The :fox jumped over the :gate into the field';
    var re = new RegExp(/:[a-z]+/i);
    var m = str.replace(re,'cow');
    alert(m); //outputs 'The cow jumped over the :gate into the field'
    
     
  4. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    Ah, of course, you need the g modifier in there to make it replace all instances; my bad :)

    Code:
    'The :fox jumped over the :gate into the field'.replace(/:[a-z]+/gi, 'cow');
     
  5. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    Almost there!

    Code:
    var str = 'The :fox jumped over the :gate into the field';
    var re = new RegExp(/:[a-z]+/ig);
    var m = str.replace(re,"'+ $('#$&').val()+'");
    alert(m);
    
    I just need to work out how to remove the : from the output.. in PHP I would have used preg_replace_callback to remove the colon, but there doesn't seem to be this sort of functionality in javascript..
     
  6. Dj_Jestar

    Caporegime

    Joined: 18 Oct 2002

    Posts: 28,940

    Location: Back in East London

    and probably the m modifier if parsing paragraphs and not just single lines. :)

    also this pattern should work:
    Code:
    /(:\w)+/igm
     
  7. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    Hi there,

    Unfortunately all that seems to do is take the first letter of each match.. f, g etc..
     
  8. Dj_Jestar

    Caporegime

    Joined: 18 Oct 2002

    Posts: 28,940

    Location: Back in East London

    try without the group..
    Code:
    /:\w+/igm
     
  9. toastyman

    Gangster

    Joined: 30 Dec 2005

    Posts: 421

    Sorted :D

    Code:
    var str = 'The :fox jumped over the :gate into the field';
    var re = new RegExp(/:\w+/igm);
    var m = str.replace(re,function(mo,key,value) {
    return "'+$('#"+mo.substr(1)+"').val()+'"
    });
    alert(m);
    
    Cheers guys!