Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a long dash from a string in JavaScript?

I've come across an error in my web app that I'm not sure how to fix.

Text boxes are sending me the long dash as part of their content (you know, the special long dash that MS Word automatically inserts sometimes). However, I can't find a way to replace it; since if I try to copy that character and put it into a JavaScript str.replace statement, it doesn't render right and it breaks the script.

How can I fix this?

The specific character that's killing it is —.

Also, if it helps, I'm passing the value as a GET parameter, and then encoding it in XML and sending it to a server.

like image 995
cd6 Avatar asked May 03 '12 17:05

cd6


People also ask

How do I remove a character from a string in JavaScript?

JavaScript String replace() The replace() method is one of the most commonly used techniques to remove the character from a string in javascript. The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the character to replace it with.

How do I remove something from a string in JavaScript?

To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed. Copied!

How do you make a long dash in JavaScript?

Simply enter two dashes (hyphens) for an en–dash, and three for an em–dash!

Can we use \n in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.


1 Answers

This code might help:

text = text.replace(/\u2013|\u2014/g, "-");

It replaces all – (–) and — (—) symbols with simple dashes (-).

DEMO: http://jsfiddle.net/F953H/

like image 165
VisioN Avatar answered Oct 06 '22 14:10

VisioN