Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing soft hyphens from a string

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.

like image 631
BeraCim Avatar asked Apr 30 '12 08:04

BeraCim


People also ask

How do you remove hyphens from a string?

Use the String. replace() method to remove all hyphens from a string, e.g. const hyphensRemoved = str. replace(/-/g, ''); . The replace() method will remove all hyphens from the string by replacing them with empty strings.

What does a soft hyphen do?

The soft hyphen tells the user agent where a line break can occur. When the word isn't wrapped (i. e., isn't broken across lines), the soft hyphen isn't visible. In this context, the soft hyphen may also be called a discretionary hyphen or optional hyphen.

What is soft hyphen in word?

A soft hyphen is an option for hyphenation. That is, it will remain invisible and will have no effect on the text, unless it is at the end of the line that must be hyphenated, when it will be used to determine how to hyphenate the word.

How do I remove a hyphen from a string in Python?

Use the str. replace() method to remove the hyphens from a string, e.g. result = my_str. replace('-', '') . The replace() method will remove the hyphens from the string by replacing them with empty strings.


1 Answers

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,'');
like image 81
Phrogz Avatar answered Sep 30 '22 12:09

Phrogz