Escape backslash followed by apostrophe

S

Sisilla

Hello All,

I have a String object called "cargotype" that is assigned a value of
"20' Standard Container". I want to pass the cargotype object to a
javascript function, so I need to add a backslash before the
apostrophe to escape it. Ideally, I would like to pass "20\' Standard
Container" to the javascript function. I thought the following would
work-:

cargotype = cargotype.replaceAll("'", "\\'");

What am I doing wrong here? I appreciate any effort to help me. Thank
you for your time and consideration.

Sincerely,

Sisilla
 
D

Daniel Pitts

Sisilla said:
Hello All,

I have a String object called "cargotype" that is assigned a value of
"20' Standard Container". I want to pass the cargotype object to a
javascript function, so I need to add a backslash before the
apostrophe to escape it. Ideally, I would like to pass "20\' Standard
Container" to the javascript function. I thought the following would
work-:

cargotype = cargotype.replaceAll("'", "\\'");

What am I doing wrong here? I appreciate any effort to help me. Thank
you for your time and consideration.

Sincerely,

Sisilla
replaceAll takes an Regex, and does a regex replacement. Try "\\\\'".
I don't know that it'll work, so you'll have to try that yourself.

Basically, what that does is passes in the string \\' to the regex
handler, which escapes the \, and passes the \' to the javascript.
 
S

Sisilla

Thank you, Daniel. It worked like a charm, and I really appreciate it.

Thanks,

Sisilla
 
C

Christian

Daniel said:
replaceAll takes an Regex, and does a regex replacement. Try "\\\\'". I
don't know that it'll work, so you'll have to try that yourself.

Basically, what that does is passes in the string \\' to the regex
handler, which escapes the \, and passes the \' to the javascript.
the first argument is a regexp not the second..
cargotype = cargotype.replaceAll(Pattern.quote("'"), "\\'");
should do the trick..
 
D

Daniel Pitts

Christian said:
the first argument is a regexp not the second..
cargotype = cargotype.replaceAll(Pattern.quote("'"), "\\'");
should do the trick..
The second one is the regex replacement, which isn't a normal string
either. \1 represents the first captured group, etc...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top