Results 1 to 7 of 7

Thread: Regexp help!!

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Regexp help!!

    Hello everyone,

    Can anyone tell me how to convert something like this: "width 100 ;height 200" to "width: 100 ;height: 200" using regular expressions or something? I have been trying this for days but couldn't succeed. Any help would be greatly appreciated.
    Thank you for your time reading this post.

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there shachi,

    does this help...
    Code:
    
    <script type="text/javascript">
      var pattern='width 100';
      var output=pattern.replace(/\s/,':');
      alert(output);
    </script>
    
    coothead

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi View Post
    Can anyone tell me how to convert something like this: "width 100 ;height 200" to "width: 100 ;height: 200" using regular expressions or something?
    Code:
    string.replace(/(^|\s|;)([^:;]+)(\s*:\s*|\s+)([^;]+)(;|$)/g, '$1$2: $4$5')
    where string is a string value. The return value will be the corrected string.

    Mike

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    coothead: Thanks but it was not what I wanted. Thanks again.

    mwinter: It worked but unfortunately I had a problem, when the string is something like

    Code:
    width 100px ;height 100px ;border 7px solid blue ;backgroundColor #99ccff ;fontFamily sans-serif ;padding 10px ;position absolute
    it converts it to
    Code:
    width: 100px ;height 100px ;border 7px solid: blue ;backgroundColor #99ccff ;fontFamily: sans-serif ;padding 10px ;position: absolute
    due to which I am having some problems. Are there any fixes for this? Thanks for your help again.

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry about that. I'd missed a couple of scenarios, obviously.

    Change the regular expression to:

    Code:
    /(^|\s|;|)([^:;\s]+)(\s*:\s*|\s+)([^;]+)(;|$)/g
    The first alteration is allow an empty match at the start to cope with semicolon flush with the next declaration. The semicolon is consumed in the preceding match, so the next match must be able to continue immediately. The second alternation excludes white space in the property name - something that I overlooked, somehow - preventing multiple, space-separated values from being considered part of the property name.

    That should do it. By the way, two of your property names - backgroundColor and fontFamily - seem to be incorrect. Was that intentional?

    Mike

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mwinter
    By the way, two of your property names - backgroundColor and fontFamily - seem to be incorrect. Was that intentional?
    I think you mean it must be background-color and font-family? If so, yes it was intentional because all this data gets parsed by javascript and I couldn't bother to converting all selector-case to CamelCase, Is there any fix for that too?

    The new regexps works great. Thanks again and thanks a lot, you're a regexp genius.

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi View Post
    I think you mean it must be background-color and font-family? If so, yes it was intentional because all this data gets parsed by javascript and I couldn't bother to converting all selector-case to CamelCase, Is there any fix for that too?
    Yes, though JScript 5.1 and earlier makes this exceedingly difficult due to the poor implementation of regular expressions. It should be possible to replace and emulate the necessary features, but it requires quite a bit of work.

    Mike
    Last edited by mwinter; 11-28-2006 at 12:20 AM. Reason: I didn't realise the full extent of the problems with JScript

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
  •