Why doesn't the decimal point symbol work in a regular expression? . - (decimal point) matches any character other than line breaks.

  var reg2 = /([\s.]+$)/ig; var match2; while (match2 = reg2.exec("drgdrgdgdrgdgr ыуаыуаыуа ыуаыаыа")) { console.log(match2[1]); } 

The result - does not find the characters according to the decimal point.

It is necessary to find the line according to . and the \s symbol \s That's about it - /([,\w!\s]+)$/ig

Only something like this - /([.\s]+)$/ig (/. /([.\s]+)$/ig - but the decimal symbol . does not work in [].


here is the expression that works - / ([, A-Yaa-i \ w! \ s] +) $ / ig Only the same thing is needed with a decimal point. Why should I list all the characters between [] that the dot supports.


1. This screen demonstrates the expression that I made up (this is how it should work) -

/([,А-Яа-я\w!\s]+)$/ig

This is a working example - a working example

Picture with correct expression work

  var reg2 = `/([,А-Яа-я\w!\s]+)$/ig`; var match2; while (match2 = reg2.exec("Тест А сейчас будет перенос на новую строку это наш текст на новой строке")) { console.log(match2[1]); } 

2. Option (this is not true) -

/(\s+|.+)$/ig

Demo Code 1

The picture with the incorrect operation of the expression number 1

  var reg2 = `/(\s+|.+)$/ig`; var match2; while (match2 = reg2.exec("Тест А сейчас будет перенос на новую строку это наш текст на новой строке")) { console.log(match2[1]); } 

3. Option (this is not true) -

/(\s+|.+)$/igm

The picture with the incorrect operation of the expression number 2

  var reg2 = `/(\s+|.+)$/igm`; var match2; while (match2 = reg2.exec("Тест А сейчас будет перенос на новую строку это наш текст на новой строке")) { console.log(match2[1]); } 
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

Inside [] point is not regarded as a special character, but as a character of a point, therefore this regular expression searches for text consisting of points and whitespace characters.

To solve, you can opt out of [] , in favor of | like so

 var reg2 = /(\s+|.+)$/ig; var match2; while (match2 = reg2.exec("rgrgr rgrgrg rgrgrgrgrg")) { console.log(match2[1]); } 

Upd. for the added example, you can remove the string limit $

 var $text_area = $('textarea'), htmlParser, chat_description2_preview = $('.chat_description2-preview'); function check() { chat_description2_preview.text(""); tested($text_area.val()); } $text_area.on('input propertychange', check); function tested(text) { var reg2 = /(.+|\s+)/ig; var match2; while (match2 = reg2.exec(text)) { if (match2[1] != undefined) { //ссылка в конце строки или просто строка без изображения chat_description2_preview.append(match2[1]); console.log(match2[1]); } } } check(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Пример номер 1</div> <textarea rows="10" cols="45" name="text"> Тест А сейчас будет перенос на новую строку это наш текст на новой строке и еще раз и еще и опять и снова </textarea> <span></span> <div style="white-space: pre;" id="con_pred" class='chat_description2-preview'></div> 

  • I added my question. Unfortunately, but your varinat will not work. Does not take into account the transition to a new line. - gilo1212
  • one
    @ gilo1212, add to the question the input and output data for which the current expression is not suitable - Grundy
  • @ gilo1212, in order for the expression in the response to work with line feed, you need to remove the end-of-line character $ - iksuy
  • @iksuy, Not necessarily, you can add another flag, but without a specific example from the author with the expected result, one can only guess what he wanted to see :) - Grundy
  • @Grundy, yes, indeed, the m flag solves the problem - iksuy