PDA

View Full Version : Regexp help!!



shachi
11-26-2006, 09:44 AM
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.

coothead
11-26-2006, 10:53 AM
Hi there shachi,

does this help...


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

coothead

mwinter
11-26-2006, 08:41 PM
Can anyone tell me how to convert something like this: "width 100 ;height 200" to "width: 100 ;height: 200" using regular expressions or something?



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

shachi
11-27-2006, 04:23 PM
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



width 100px ;height 100px ;border 7px solid blue ;backgroundColor #99ccff ;fontFamily sans-serif ;padding 10px ;position absolute

it converts it to


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.

mwinter
11-27-2006, 04:41 PM
Sorry about that. I'd missed a couple of scenarios, obviously.

Change the regular expression to:



/(^|\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

shachi
11-27-2006, 05:34 PM
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.

mwinter
11-27-2006, 07:22 PM
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