Web Analytics Made Easy -
StatCounter regular expresion split - CodingForum

Announcement

Collapse
No announcement yet.

regular expresion split

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • regular expresion split

    Hi, I am having an anoying time with regular expresions. I am trying to split a string up were non alpha numberic characters are ( \W ) but the problem is that it removes what I want as the split point. I want to split something like "hello everybody, how are you" into the array: "hello"," ","everybody",","," ","how"," ","are"," ","you" but the problem is if i do a split were it splits, it removes that character so if i did a split for \W on "hi everybody" id get "hi","everybody" I would like to have a way to keep this character and place it in the array using regular expresions if posible.

    Thanks for any help
    Kris Hubby
    kwhubby site

  • #2
    There's two things you can do:
    - Capture the non-alpha /(\W)/g. It will then be in the match array afterwards. The drawback with this is that only the modern browsers support it, so you need another approach if you want to support any version of ie.
    - Match word boundaries, /\b/g, instead of non-alpha characters.
    liorean <[[email protected]]>
    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

    Comment


    • #3
      ah ha! thanks alot! I had tried adding the parenthesis and the multiline flag but I was using IE so I had no luck.. thanks for the other way that really helps!
      Kris Hubby
      kwhubby site

      Comment


      • #4
        Originally posted by liorean
        There's two things you can do:
        - Capture the non-alpha /(\W)/g. It will then be in the match array afterwards. The drawback with this is that only the modern browsers support it, so you need another approach if you want to support any version of ie..
        Liorean - Could you explain more please? /\W/g works fine for me in IE 5.5. It is the same as [^a-zA-Z-0-9].

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          Yes it does, but if you reread the earlier posts you will see that wasn't what problem we were talking about. The problem is this:
          Code:
          [color=green]// Modern browsers:[/color]
          'hey there, are you awake?'.split(/(\W)/g);
              [color=green]// => [
                  'hey',
                  ' ',
                  'there',
                  ',',
                  '',
                  ' ',
                  'are',
                  ' ',
                  'you',
                  ' ',
                  'awake',
                  '?',
                  ''
              ][/color]
          
          [color=green]// Internet Explorer (any version, any platform):[/color]
          'hey there, are you awake?'.split(/(\W)/g);
              [color=green]// => [
                  'hey',
                  'there',
                  'are',
                  'you',
                  'awake'
              ][/color]
          
          [color=green]// Which you can compare to this:[/color]
          'hey there, are you awake?'.split(/\W/g);
              [color=green]// => [
                  'hey',
                  'there',
                  'are',
                  'you',
                  'awake'
              ][/color]
          Thus, you lose all non-word characters in ie.

          Instead, you may use this. It's not quite the same, but it's as close as you're going to get:
          Code:
          'hey there, are you awake?'.split(/\b/g);
              [color=green]// => [
                  'hey',
                  ' ',
                  'there',
                  ', ',
                  'are',
                  ' ',
                  'you',
                  ' ',
                  'awake',
                  '?'
              ][/color]
          Last edited by liorean; Mar 2, 2004, 06:35 AM.
          liorean <[[email protected]]>
          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

          Comment

          Working...
          X