I'm doing some stuff with javascript and I'm wondering, how do I put an apostrophe in a string in javascript?
theAnchorText = 'I apostrophe M home';
In this case, an apostrophe after a backslash means to use a literal apostrophe but not to end the string. There are also other characters you can put after a backslash to indicate other special characters. For example, you can use \n for a new line, or \t for a tab.
Using the Escape Character ( \ ) We can use the backslash ( \ ) escape character to prevent JavaScript from interpreting a quote as the end of the string. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.
replace(/'/g, 'A');
You can use double quotes instead of single quotes:
theAnchorText = "I'm home";
Alternatively, escape the apostrophe:
theAnchorText = 'I\'m home';
The backslash tells JavaScript (this has nothing to do with jQuery, by the way) that the next character should be interpreted as "special". In this case, an apostrophe after a backslash means to use a literal apostrophe but not to end the string.
There are also other characters you can put after a backslash to indicate other special characters. For example, you can use \n
for a new line, or \t
for a tab.
You can try the following:
theAnchorText = "I'm home";
OR
theAnchorText = 'I\'m home';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With