$('document').ready(function() { $('#city').on('keyup', function() { var inputVal = $('#city').val(); var Reg = / /; Reg = /[inputVal]/; // ВОПРОС НА ЭТОЙ СТРОКЕ! REGEX $.ajax({ type: 'GET', url: 'https://api.myjson.com/bins/1b9f7n', dataType: "json", success: function(data, textStatus) { $.each(data, function(i, val) { if (inputVal.length > 1 && Reg.test(data[i])) { $('#cities').append('<li class="city">' + data[i] + '</li>'); } }) }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("some error"); } }); }); }); 
  • one
    learn.javascript.ru/regexp-introduction new RegExp("шаблон", "флаги") - Visman
  • What you need is not there. - Pavel
  • It is necessary for Regular to change dynamically with each event. - Pavel
  • So you can simply store the regular expression as a string, and each time create a new regular expression based on the string. - Dmitry Polyanin
  • @Pavel, everything is there. You form a relya as a string, each time a different one and create a new RegExp based on the string. - Visman

2 answers 2

Upd. The most obvious option

Perhaps you missed the most obvious option.

I will assume that you have an algorithm for dynamically determining what the regular expression for a specific field should be. In this case, you can simply simply override at each loop iteration the Reg variable, which contains the pattern to be checked. If your regular expression templates are stored as strings, then you can convert them to regular expressions by calling the RegExp constructor, passing it the same template with the first parameter and flags (if necessary) with the second:

 Reg = new RegExp("шаблон", "флаги"); 

That is, in the end, you can get something like this:

 $.each(data, function(i, val) { Reg = new RegExp("шаблон", "флаги"); // * if (inputVal.length > 1 && Reg.test(data[i])) { $('#cities').append('<li class="city">' + data[i] + '</li>'); } }) 

The only thing that falls on your shoulders is how you will receive the “new” template.

Theory

Using the scientific method, it was found out that even though the regular expression pattern is stored in the pattern property of the RegExp object, which can be changed by simple redefinition, even after the redefinition the regular expression itself will not work any other way.

What is it about?

For example, there is a regular expression that is defined as:

 var reg = /(\w+)\s(\w+)/; 

At the same time, the pattern itself as a string is stored in its property pattern :

 console.log(reg.pattern) // => "(\w+)\s(\w+)" 

If you override it:

 reg.pattern = "(\w+)"; 

That pattern property is indeed redefined:

 console.log(reg.pattern) // => "(\w+)" 

However, this does not affect the operation of the regular expression itself, since its literal remains unchanged. Consequently,

 reg.test("John") // => false 

but

 reg.test("John Smith") // => true 

still.

From what I concluded, it is not possible to dynamically override a pattern.

By the way , my words are confirmed by the specification. The original template is stored in the [[OriginalSource]] internal property, and the source instance property is permanent and non-writable.

Other solutions

As I understand it, you have a set of fields, for each of which a certain regular expression is stored somewhere (perhaps just as a string). If this is the case, then in this case you can dynamically prepare in advance an array with regular expressions for each of the fields, and then check each individual field with a special expression for this field.

On the other hand, it is not quite clear to me where the “dynamics” comes from for the substitution of the regulars. Please clarify this point.

That is, I understand that for each of the fields you want to have your own check, but by what algorithm is it chosen?

  • And what prevents the regular expression from simply storing as a string, and creating a new regular expression each time based on this string. - Dmitry Polyanin
  • @DmitryPolyanin and this is a question for the author. I personally - nothing. But if the author has already asked a question, then I assume that he has already considered this most obvious option. - smellyshovel
  • And in vain. For some reason, obvious answers are often not visible. - Dmitry Polyanin
  • @DmitryPolyanin updated just in case. But I doubt that everything is so simple. - smellyshovel
  • In fact, everything turned out to be very simple, they confused two variants of regular expression declarations. One option is new RegExp and another option var reg = / /; so I tried to insert a value from a variable into the second variant .... and it did not work at all ... Then I just did so var reg = new RegExp (value); and everything worked out. Thanks for the help! - Pavel
  var inputVal = $('#city').val(); var Reg = new RegExp(inputVal); 

THAT'S WHAT I AM SEEKED. Initially I tried to initialize via / / (RegEx object) and then somehow insert it there ... Everything is much easier with the help of new RegExp (value)