Detect Line Break


I have the following function:

  if (str.match(/\d\d\d\d\d\.\d\d\d\d\d/)) {
    var codigo_velho = str.match(/\d\d\d\d\d\.\d\d\d\d\d\/);
    result = "1"; }

How do I change this function so that it detects line break in match ? For example, I tried with \n but it did not work:

 if (str.match(/\d\d\d\d\d\.
                         \d\d\d\d\d/)) {
    var codigo_velho = str.match(/\d\d\d\d\d\.
    result = "1"; }
asked by anonymous 26.05.2014 / 04:34

4 answers


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 .

27.05.2014 / 00:22

Do not use literal line breaks, use \n and a safeguard for \r : \r?\n . This would work with Unix-style ( \n ) and Windows-style ( \r\n ) breaks. The part of match would look like this:


Or a little shortening with the Leonardo Bosquett tip :

26.05.2014 / 05:19

Line breaks can be different formats. The best way to "catch" them is to be comprehensive in regex.

Tip: string.match(/(\r\n|\n|\r)/gm);

The \n selector is the essentials. It's worth reading this answer about differences between \n and \r . I then used g to return all the results, not only the promising one, but also the m that means multi-lines. In the example I put below is not necessary but as I do not know exactly how it will use, I put it.

Running example: link


<div id="div1">Quebra de linha 1: 
    - continua aqui</div>
<div id="div2">Quebra. <br />Quebra. 

    Quebrão :)</div>
<div id="div3">Nenhuma quebra</div>


var mensagem = '';
$('div').each(function () {
    var string = this.innerHTML;
    var linhas = string.match(/(\r\n|\n|\r)/gm);
    var quantidadeLinhas = linhas ? linhas.length : 0;
    mensagem += 'A div ' + + ' tem ' + quantidadeLinhas + ' quebras de linha\n';

The alert this gives is:


Div div1 has 1 line breaks   Div div2 has 6 line breaks
  Div div3 has 0 line breaks

26.05.2014 / 09:50

The line break test in Javascript by Regex is \ n as Wakin said, see the test on the Chrome console:


varnewExpression=expression.replace("\r\n", "\n");

Or simply remove them from validation if they are not needed:

var newExpression = expression.replace("\r\n", "");

Tip: expressions like \ d \ d \ d \ d can be simplified by [\d]{4}

I also indicate this site to test your regular expressions: link

26.05.2014 / 05:10