How to replace with single quote ?

The following expression replace a
forward slash with two single quotes

i want only to one single quote


item_name.replace(/\\/g,"\'");
LVL 8
PeteEngineerAsked:
Who is Participating?
 
CombatGold1Connect With a Mentor Commented:
Instead of \' see if specifying the single-quote's hexadecimal reference works, which would be \0x27, or the octal reference \047.
0
 
sjklein42Connect With a Mentor Commented:
Your code seems to work fine:

(The code actually replaces backslashes \ , not forward slashes / .  Is that what you meant to say?)

<script>
var item_name = 'abc\\def'
alert(item_name)
item_name = item_name.replace(/\\/g,"\'");
alert(item_name)
</script>

Open in new window

0
 
käµfm³d 👽Connect With a Mentor Commented:
Perhaps you mean you want to replace a series of one-or-more slashes with just one single quote?
item_name.replace(/\\+/g,"\'");

Open in new window

0
 
grouper15Connect With a Mentor Commented:
can you send the value of item_name you are having problem with
0
 
leakim971Connect With a Mentor PluritechnicianCommented:
No need to escape the simple quote inside the double quote
item_name.replace(/\\/g,"'");

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.