7

Let's say I have a string that is littered with soft hyphens (pretend that the hyphens in the text below are soft hyphens):

T-h-i-s- -i-s- -a- -t-e-s-t-.-

I want to remove the soft hyphens and only return the string of:

This is a test.

I'm trying to do this in JavaScript. Below is the farthest I have gotten so far:

RemoveSoftHyphen: function (text) {
    var result = text.match(/[^\uA00AD].*/);
    alert (result);
    return result;
}

When the alert displayed, all I got was a "-", which I'm not sure whether thats a soft or hard hyphen... but more importantly, it didn't work.

I'm trying to find out what is wrong with my regex, or is there a better approach to removing soft hyphens which I'm not aware of using either JavaScript or jQuery.

William Perron
  • 485
  • 7
  • 16
BeraCim
  • 2,317
  • 8
  • 49
  • 78
  • 1
    Where in the code does the string get inserted back into the DOM? – Hamish Apr 30 '12 at 08:08
  • @Hamish: this is a function that would be called by something else. The matching of the soft hyphen is the most worrying part, as I'm out of ideas on exactly how to get rid of those ninja hyphens and return me the sanitized string. – BeraCim Apr 30 '12 at 08:41
  • But *"the returning string doesn't contain any soft hyphens anymore"*. It's not clear what you're asking. Perhaps create an example at jsfiddle.com – Hamish Apr 30 '12 at 08:50
  • What you want to do is - Remove a hyphen from a string, is it? – Nikhil Baliga Apr 30 '12 at 09:38
  • Thanks for all the help so far. I have updated the question. Hopefully it'll be a bit clearer. – BeraCim Apr 30 '12 at 23:46

1 Answers1

14

Assuming that the character really is Unicode 00AD:

var result = text.replace(/\u00AD/g,'');

If you have many hyphens to kill, use a character class:

var result = text.replace(/[\u00AD\u002D\u2011]+/g,'');
Phrogz
  • 296,393
  • 112
  • 651
  • 745