javascript regex pattern with an array -


I want to create a regex pattern which should be able to search through an array.

We say:

  var arr = ["first", "second", "third"]; var match = text.match (/ & lt; ara & gt; /);   

who should only be able to match

  & lt; First & gt; Or & lt; Seconds & gt; Or & lt; Third & gt; ......   

but should ignore

& lt; ffirst & gt; Or & lt; Dummy & gt;

I need an efficient approach.

Any help would be great thanks

You (a | b | c) should search for a particular word .

. has been created from arr by joining the values ​​as <

var arr = ["first", "second", "third"]; var match = text.match (new regxp ("& lt; (?:" + arr.join ("|") + "))");); // mail & lt; First & gt; & Lt; Second & gt; And & lt; Third & gt;

Note that if your "source" words contain regular characters of regular expressions - you may have trouble - so you may need to avoid those characters which Before joining

A good job can be found to do this:

  function regexpQuote (str, delimiter) {return string (str). Replace (New RegExp ('[.. \\\\ + * * \\ [\\ ^ \\] $ () {} =!  | |: \\' + (Limiter || '' ) + '-]', 'g'), '\\ $ & amp;'); }   

So in this case you have

  function ascenderer (ARR) {var escaped = []; For (arr in var i) {escaped.push (regexpQuote (arr [i])); } The refund survived; } Var arr = ["first", "second", "third"]; var pattern = new regExp ("& lt; (?:" + escape arr .joint ("|") + ") & gt;"); var match = text.match (pattern); // mail & lt; First & gt; & Lt; Second & gt; And & lt; Third & gt;    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -