The question has already been widely answered, so I'll just 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()
Searches the a regular expression and a specified string . Returns true or false . Already .match()
is used to get the results by combining a string against a regular expression. Returns an array with the results or null if there are none. If the string does not have a result, then the boolean value will be false .
Or MDN quotes the following about:
If you need to know if a string matches an expression
regular, use RegExp.test (str).
Is there a significant difference in performance? According to the JsPerf , yes, the difference is approximately 30% ~ 60% depending the browser. Details removed null == false
.