Link to home
Start Free TrialLog in
Avatar of ecchymose
ecchymose

asked on

RegExp problem with double-quote (")

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?
Avatar of jaysolomon
jaysolomon

try this

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

ASKER

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
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...
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>
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
Avatar of Zvonko
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>


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?
Try this:

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

Same problem!

The single quote is sometimes used in the text...
How about this:

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


Oh!  Nice solution!

Thanx to all, I'll take the Zvonko solution...
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!
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?
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!
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...
You are right, I just tested NS4.7; It ignores "display:none"
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooooh!  This one is really great!  Ok, you got it!
<|:-)