The question has already been widely answered, so I will only make a suggestion, instead of using .match()
to detect line breaks, use .test()
to check whether or not the test content contains line breaks, something like this:
verificarQuebra = function()
{
var valor = $('#div1').html();
if (/[\n|\n\r]/.test(valor))
{
alert("Existem quebras de linha!");
} else
{
alert("Não existem quebras de linha!");
}
}
Demonstration aqui
.
There is a difference between .match()
and .test()
?
The function .test()
Search between a regular expression and a string specified. Returns true or false. Already .match()
is used to obtain the results by combining a string against a regular expression. Returns a array with the results or void if there is none. Soon null == false
if the string does not have a result, the boolean value will be false.
The MDN cites the following on:
If you need to know if a string corresponds to an expression
regular, use Regexp.test(str).
Is there a significant difference in performance? According to Jsperf, yes, the difference is approximately 30% ~ 60% depending on the browser. Details taken dessa resposta
.
Have you tested with the n expression inside the regex? Like: d d n d...? I know that breaking the line doesn’t work.
– Wakim
Tried to
\r\n
? Line break is half variable...– Felipe Avelar
@Felipeavelar, tried r n did not work..
– user7605
@Wakim, unfortunately did not. Because the code line that comes in MATCH, some of them jump line. type 222222 n222222, when it happens it does not detect.
– user7605