RegExp problem with double-quote (")

ecchymose
ecchymose used Ask the Experts™
on
I have this code:

givenPattern="&var[0-9]+=";
myRegExp = new RegExp(givenPattern,"i");
chunks = document.frm.textfield.value.split(myRegExp);

Then I test it with 2 different texts in "textfield":

Test A)

&comment=### start ###
&var1=Hello
&var99=Cool
&comment=### end ###

Test B)

&comment=### start ###
&var1="Hello"
&var99=Cool
&comment=### end ###

---
Notice double-quotes in Test B)

Then if I alert(chunks[1]); with A) it works... Testing it with B) says chunks[1] is Undefined.

What's wrong with those double-quotes?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try this

&comment=### start ###
&var1=\"Hello\"
&var99=Cool
&comment=### end ###

Author

Commented:
well...  problem is those texts are in fact text files...  loaded in a textfield after...

But those text files content must be loaded in some Flash tool...  So I don't want my variable content to print the backslashes!
Hmm

backslash escapes the quotes

like this

<script>
alert(".......\"Hello\" ......World");
</script>

Is this actionscript or what
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
yeah, but it's more complicated... example was to simplify this a LOT...

For some reasons, I don't want to change the content of the text files...  

I'd prefer a solution "script-side" instead of "content-side", If I can say...

Commented:
Hi,

I used the code below to test it (I use Internet Explorer 6.0) and I can't seem to get the error, can you post some more information about the other code?

Thanks,
Drackon

<HTML>
<head>
  <script>
    function doSplit() {
      givenPattern="&var[0-9]+=";
      myRegExp = new RegExp(givenPattern,"i");
      chunks = document.frm.textfielda.value.split(myRegExp);
      alert(chunks[1]);
      chunks = document.frm.textfieldb.value.split(myRegExp);
      alert(chunks[1]);
    }

  </script>
</head>
<body>

<form name=frm>
<textarea name=textfielda>
&comment=### start ###
&var1=Hello
&var99=Cool
&comment=### end ###
</textarea>

<textarea name=textfieldb>
&comment=### start ###
&var1="Hello"
&var99=Cool
&comment=### end ###
</textarea>
<BR>
<a href=# onClick="doSplit()">GO</a>
</form>

</body>
</HTML>

Commented:
After you have got the textfield data you could then use this:

var txt = document.frm.textfieldb.value.replace(/\"/,"\\\"");

and then do the split.  The function just replaces the " with \" after you have loaded the information
ZvonkoSystems architect
Top Expert 2006

Commented:
The split() works as expected.
You have a typo elsewhere.

Here my test:

<html>
<head>
<script>
function showChunks(theText){
givenPattern="&var[0-9]+=";
myRegExp = new RegExp(givenPattern,"i");
chunks = theText.split(myRegExp);
alert(chunks);
alert(chunks[1]);
}
</script>
</head>
<body>
<form>
<textarea onClick="showChunks(this.value)" rows=10>
&comment=### start ###
&var1=Hello
&var99=Cool
&comment=### end ###
</textarea>
<textarea onClick="showChunks(this.value)" rows=10>
&comment=### start ###
&var1="Hello"
&var99=Cool
&comment=### end ###
</textarea>
</form>
</body>
</html>


Author

Commented:
Well, problem is that the content is not in a textfield, but in a hidden filed, (sorry)

So I guess the error the value attribute is closed when the first " appears...

Input type="hidden" value="&comment=### start ###
&var1="Hello"
&var99=Cool
&comment=### end ###
"

As I see it, I won't have any other choice than escaping the double slashes in ALL text files...

But is there any other solution "script-side" you see?
ZvonkoSystems architect
Top Expert 2006

Commented:
Try this:

<Input type="hidden" value='&comment=### start ###
&var1="Hello"
&var99=Cool
&comment=### end ###
' >

Author

Commented:
Same problem!

The single quote is sometimes used in the text...
ZvonkoSystems architect
Top Expert 2006

Commented:
How about this:

<textarea name="textfield" style="display:none">
&comment=### start ###
&var1="Hello"
&var99=Cool
&comment=### end ###
</textarea>


Author

Commented:
Oh!  Nice solution!

Thanx to all, I'll take the Zvonko solution...

Author

Commented:
No, in fact I can't take Zvonko's solution because it's a hidden field and not a textarea...

I'll change all double-quote by a Flash readable symbol and "find and replace" after to show it...

Thanx!
ZvonkoSystems architect
Top Expert 2006

Commented:
Can you explain the difference between hidden <input> field and hidden <textarea> field?
You access the values by same syntax, so what is the problem for you?

Author

Commented:
I think a hidden field, as it is hidden, don't have any display properties concerning CSS...

Anyway, as it was kinda urgent, I couldn't test much things, and I decided it was faster to automatically find-and-replace double quotes by some Flash readable character replacing double-quotes...

Thank you again!

Author

Commented:
Oh!  Okay, I see...  displat:none HIDES the Textarea...

Well, ok, it's better that way...  But anyway, the tool I'm working on must be Netscape 4.x compatible...  and I don't want take risks with CSS and Netscape anyway...
ZvonkoSystems architect
Top Expert 2006

Commented:
You are right, I just tested NS4.7; It ignores "display:none"
Systems architect
Top Expert 2006
Commented:
Touchdown! I got it!

Use this:

<html>
<body>
<form>
<input type=hidden name="textfield" value="&comment=### start ###
&var1=&quot;Hello&quot;
&var99=Cool
&comment=### end ###">
</form>
</body>
</html>

Tested in NS4.7 <|:-)



Author

Commented:
Ooooh!  This one is really great!  Ok, you got it!
ZvonkoSystems architect
Top Expert 2006

Commented:
<|:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial