Skip to main content
Mahesh_JW
Inspiring
July 30, 2010
Question

RegExp help

  • July 30, 2010
  • 2 replies
  • 546 views

Hi All,


var linkname = "0012.picture.eps";
var FLG;
var art_extn = ["psd","tif","eps","jpg","ai","pdf"];

FLG = 0;
for(var a=0;a<art_extn.length;a++){
   
    var patt = /\."+art_extn+"$/i;
    alert(art_extn
+"\n\n"+patt);
    if(linkname.match(patt) != null){
        alert("match");
        FLG = 1;
    }
}

if(FLG == 0){
    alert("not matched");
}

i got not matched what can i do please help me.

Thanks in advance

This topic has been closed for replies.

2 replies

Muppet_Mark-QAl63s
Inspiring
July 30, 2010

or something like…

var linkname = "0012.picture.eps"; var res = linkname.match(/\.(psd|tif|eps|jpg|ai|pdf)$/i) alert(res[1]); res != null ? res = true : res = false; alert(res);

Peter Kahrel
Community Expert
Community Expert
July 30, 2010

That's indeed much better...

Muppet_Mark-QAl63s
Inspiring
July 30, 2010

From you Peter 'thanks' it was however flawed…

var linkname = "0012.picture.tif"; var res = linkname.match(/\.(psd|tif|eps|jpg|ai|pdf)$/i) if(res != null) alert(res[1]); res != null ? res = true : res = false; alert(res);

alerting 'res[1]' would fail if there was no match… but I was just trying show what it matched if needed… I think this may be better now…

Peter Kahrel
Community Expert
Community Expert
July 30, 2010

You can't use variables in a literal regex. try something like this:

patt = RegExp ("\\." + art_extn + "$", "i");
. . .
. . .
if (linkname.match (patt) != null)