SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text between delimiters (regex help!)

    Hi, suppose you have a textbox where the user's free to enter anything...

    When the user enters text surrounded by delimiters, such as:

    Code:
    a bunch of text here, and then more text.
    now suddenly:
     #some text here#
    
    and then some more text here and "there" and etc..
    how do you fetch the text surrounded by the delimters # and #.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var str=inputelement.value;

    var A= str.match(/#[^#]+#/g)
    returns an array (includes the delimeters)

    or without delimeters:

    var A=[],pattern;
    while((pattern=/#([^#]+)#/g.exec(str))!=null){
    A.push(pattern[1]);
    }
    return A
    Last edited by mrhoo; Feb 13, 2007 at 22:28.

  3. #3
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't work...

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var str='some text #some more text# some text #really#';

    var A= str.match(/#[^#]+#/g);
    alert(A) //returns '#some more text##really#;

    var A=[],pattern;
    while((pattern= /#([^\#]+)#/g.exec(str)) != null){
    A.push(pattern[1]);
    }
    alert(A) //returns 'some more text,really'


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
  •