Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim &nbsp values in javascript

I am trying to trim the text which I get from kendo editor like this.

var html = "  T  "; // This sample text I get from Kendo editor
            console.log("Actual :" + html + ":");
            var text = "";
            try {
                // html decode
                var editorData = $('<div/>').html(html).text();
                text = editorData.trim();                    
                console.log("After trim :" + text + ":");
            }
            catch (e) {
                console.log("exception");
                text = html;
            }

This code is in seperate js file ( generated from typescript). When the page loads the trimming is not working. But when I run the same code in developer tools console window its working. Why it is not working?

Adding typescript code

 const html: string = $(selector).data("kendoEditor").value();
        console.log("Actual :" + html + ":");
        let text: string = "";
        try {
            // html decode
            var editorData = $('<div/>').html(html).text();
            text = editorData.trim();
            console.log("After trim :" + text + ":");
        }
        catch (e) {
            console.log("exception");
            text = html;
        }
like image 788
PSR Avatar asked May 23 '16 09:05

PSR


People also ask

What do you mean by the trim?

to put into a neat or orderly condition by clipping, paring, pruning, etc.: to trim a hedge. to remove (something superfluous or dispensable) by or as if by cutting (often followed by off): to trim off loose threads from a ragged edge. to cut down, as to required size or shape: trim a budget; trim a piece of wood.

What does having a trim body mean?

If you describe someone's figure as trim, you mean that it is attractive because there is no extra fat on their body. [approval] The driver was a trim young woman of perhaps thirty. Synonyms: slender, fit, slim, sleek More Synonyms of trim.

Why is it called trim?

Trim is slang for female genitalia. In the blues, it's usually used by a man to express a need or an intention, as in “I'm gonna get me some trim tonight.” This usage has been around since the 1920s. This meaning for trim is still in use today.

What does trim mean in texting?

Removes all spaces from text except for single spaces between words. Use TRIM on text that you have received from another application that may have irregular spacing.


3 Answers

&nbsp; becomes a non-break-space character, \u00a0. JavaScript's String#trim is supposed to remove those, but historically browser implementations have been a bit buggy in that regard. I thought those issues had been resolved in modern ones, but...

If you're running into browsers that don't implement it correctly, you can work around that with a regular expression:

text = editorData.replace(/(?:^[\s\u00a0]+)|(?:[\s\u00a0]+$)/g, '');

That says to replace all whitespace or non-break-space chars at the beginning and end with nothing.

But having seen your comment:

When I run this piece of code separately, it is working fine for me. But in application its failing.

...that may not be it.

Alternately, you could remove the &nbsp; markup before converting to text:

html = html.replace(/(?:^(?:&nbsp;)+)|(?:(?:&nbsp;)+$)/g, '');
var editorData = $('<div/>').html(html).text();
text = editorData.trim();    

That removes any &nbsp;s at the beginning or end prior to converting the markup to text.

like image 80
T.J. Crowder Avatar answered Oct 06 '22 12:10

T.J. Crowder


To easiest way to trim non-breaking spaces from a string is

html.replace(/&nbsp;/g,' ').trim()
like image 32
smjhunt Avatar answered Oct 06 '22 12:10

smjhunt


If you are using jQuery you can use jQuery.trim()

function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. source

like image 24
mikewasmike Avatar answered Oct 06 '22 12:10

mikewasmike