Trim line breaks from a string in JavaScript without frameworks [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I trim a string in javascript?
Using JavaScript without any frameworks how would you trim line breaks? Trim as being defined by PHP, removing the leading and ending line breaks while preserving line breaks after the first non-white-space character and until though not beyond the last non-white-space character.
By default most people will want to know how to remove both leading/ending line breaks as well as white-space spaces too though some may want to retain the white-space spaces while trimming just the white-space line-breaks. It also generally helps to see two working examples and how they relate when they both work for people learning code, so I'm looking for trimming both white-space line-breaks and white-space spaces.
It'd be good also to see how to trim just the leading/ending line breaks while preserving white-space spaces (which may or may not be included in the main answer).

I think this works:
string.replace(/^\s+|\s+$/g,"");

trim in general can be defined as .replace(/^\s+|\s+$/g,''), but since you want only vertical whitespace you should use .replace(/^[\r\n]+|[\r\n]+$/g,'').

Related

Terminology: Is tab a non-breaking space?

AFAIK, nbsp (non-breaking space) is this: . But tab (\t) is also a non-breaking space right? I mean it doesn't create a new line.
If everything above is correct, then how would you call a variable that can contain either or \t ? Something ugly like tabOrNbsp?
I am asking because currently in my code a variable called nbsp is used for that purpose, but the ambiguity makes me sick. Or is it correct as it is now?
P.S. This question is so dumb, but don't hate, now I see how dumb it was. In the end the commenters and the answerer really helped to sort things out!
I think you've misunderstood the term "non-breaking space".
Normally, although a space or tab character doesn't require a line break, it allows line-wrapping. So if a paragraph goes on long enough, it will eventually spread across multiple lines, even if you use lots of spaces and tabs.
A "non-breaking space" is a space that does not allow line-wrapping; if two words have a non-breaking space between them, then those two words will always end up on the same line, even if they're at the end of a line and you would otherwise expect line-wrapping between them. In Unicode, non-breaking space is coded as a specific character, U+00A0 NO-BREAK SPACE, and in HTML, you can use the entity reference to conveniently embed this character. This character is different from the normal space character , which in Unicode is coded as U+0020 SPACE.
If I've correctly understood the idea that you have in mind, the closest term is probably "linear white space" (LWS or LWSP), which means a sequence of space or tab characters (the idea being that these are whitespace characters "within a line", not forcing a line break).

Tell Browsers to replace UTF-8 word break character with hyphen?

I just explored the great UTF-8 character \u200b, which tells Browsers where it can break a word, if it doesn't fit it's parent container:
MySuperLongWordThat\u200bWontFitItsParentContainer
will be displayed as
MySuperLongWordThat
WontFitItsParentContainer
Is there any way to tell the Browser to automatically replace \u200b with a hyphen - in case the word will break?
I thought about replacing it manually with JavaScript, but I do not know any event that will fire when the word breaks.
That's not what zero-width space is intended for.
The CSS hyphens property can be used to help, but you'll notice from that documentation that if you want to manually insert word-wrap points then you should use ­ - the "soft-hyphen".

using single quote or double quote [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
Difference between single quotes and double quotes in Javascript
When I started learning jQuery, I frequently found examples using single quote or double quote as jQuery selector:
$('#myDiv'); or $("#myDiv");
They are basically the same. But which one is used prefer and fast execute please let me know.
You'll want to use single quotes where you want double quotes to appear inside the string (e.g. for html attributes) without having to escape them, or vice-versa. Other than that, there is no difference, performance vise also both are same.
There's no performance difference. They mean the same thing. I usually use singles for property names and other short literals, and doubles for long text that the user will see. The exception of course, is when one of the two appears inside the string, then use the other.
Personally I use double quotes when working with strings (= more than one char) and single quotes when working with chars (= one char) although there is no difference in Javascript. There's also no "hidden" feature in double quotes like there is in php.
jQuery code style mandates the use of double-quotes, but there's no diference in usage.
Double quotes are typed faster =)

Detect line breaks in textarea using javascript

Anyone knows a technique or method to find the position of the natural text line breaks in a textarea using javascript? It's not about finding \n or <br/>'s.
It is not possible to do this directly, but the answer to this question uses a trick to find them where it adds characters one by one and checks for scrolling:
finding "line-breaks" in textarea that is word-wrapping ARABIC text

Javascript regex syntax for HTML5 input validation

So there have been plenty of questions, and filtering through a few, I still dont know how to go about this...
Pattern for:
Alphabets ONLY, no case sensitivity, no limit on character count or words, minimum 3 characters...
I have
pattern="[A-z]{3,}"
That gives me everything, except that I'm limited to one word only... :-(
Edit: Let me be a little more clear on what I want the validation to achieve for me...
I'm using it to capture a person's name. But I do not want any special characters or numerals involved, so no "John Doe Jr.", as the '.' will get rejected, but I want to be able to capture double, or even single character portions, whilst maintaining 'global' 3 char minimum limit...
All you have to do to allow spaces as well is to add a space to the character pattern where you have [A-z].
So it becomes:
pattern="[A-z ]{3,}"
Hope that helps.
Note, however, that this will prevent other types of white space characters. I assume this is what you want, since you're being quite restrictive with the rest of the character set, but it's worth pointing out that non-breaking spaces, carriage returns, and other white space will be blocked in the above. If you want to allow them, use \s instead of just a space: this will match any white space character.
Finally, it's worth pointing out that the standard alphabet is often insufficient even for plain English text. There are valid English words with accents, as well as apostrophes and other punctuation. You haven't specified what the field is being used for, so I'll assume this is not an issue, but I felt it was worth pointing out nevertheless.
It is difficult to see what is the question. You are matching a String, not a set of words.
If your pattern is "a list of words, each of the words alphabetical only and separated by whitespace", then the regex would be
([A-Za-z]{3,}\\s*)+
Edited to answer to updated question.
[A-Za-z\\s]*([A-Za-z]{3,})+[A-Za-z\\s]* (works in Java)
How about pattern = "[A-z\s]{3,}"?

Categories

Resources