How to detect line breaks in a text area input?

You can use match on the string containing the line breaks, and the number of elements in that array should correspond to the number of line breaks. enteredText = textareaVariableName.val(); numberOfLineBreaks = (enteredText.match(/\n/g)||[]).length; characterCount = enteredText.length + numberOfLineBreaks; /\n/g is a regular expression meaning ‘look for the character \n (line break), and do it globally … Read more

Neatest way to remove linebreaks in Perl

After digging a bit through the perlre docs a bit, I’ll present my best suggestion so far that seems to work pretty good. Perl 5.10 added the \R character class as a generalized linebreak: $line =~ s/\R//g; It’s the same as: (?>\x0D\x0A?|[\x0A-\x0C\x85\x{2028}\x{2029}]) I’ll keep this question open a while yet, just to see if there’s … Read more

How to make a line break on the Python ternary operator?

You can always extend a logical line across multiple physical lines with parentheses: answer = ( ‘Ten for that? You must be mad!’ if does_not_haggle(brian) else “It’s worth ten if it’s worth a shekel.”) This is called implicit line joining. The above uses the PEP8 everything-indented-one-step-more style (called a hanging indent). You can also indent … Read more

Correct style for line breaks when chaining methods in Python

PEP 8 recommends using parenthesis so that you don’t need \, and gently suggests breaking before binary operators instead of after them. Thus, the preferred way of formatting you code is like this: my_var = (somethinglikethis .where(we=do_things) .where(we=domore) .where(we=everdomore)) The two relevant passages are this one from the Maximum Line Length section: The preferred way … Read more