AbuMariam
AbuMariam

Reputation: 3698

Regular Expression only match if String ends with target

I need a regular expression that will only match to the String if it ends with the target that I am looking for. I need to locate a file with a specific extension, problem is this extension also comes in other files. For example I have two files named

B82177_2014-07-08T141507758Z.ccf

and

B82177_2014-07-08T141507758Z.ccf.done

I only want to grab the first of these and my pattern is:

.*\.ccf

but this grabs both.

Any suggestions appreciated, I am a newbie to regular expressions.

Upvotes: 85

Views: 289705

Answers (3)

GMKHussain
GMKHussain

Reputation: 4719

VanilaJS function base example

function isValidUrl(x){
  let expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
  let regex = new RegExp(expression);
  
  return x.match(regex) ? true : false
  
}

console.log( isValidUrl('www.domain.com') ) // true

console.log( isValidUrl('nothing') ) // false

Upvotes: -1

Shahir Ansari
Shahir Ansari

Reputation: 1848

$ is used to match the end of the string. and can be used like

"string"$

like

xyz$

if you want to end with xzy

Upvotes: 24

p.s.w.g
p.s.w.g

Reputation: 149088

Use an end anchor ($):

.*\.ccf$

This will match any string that ends with .ccf, or in multi-line mode, any line that ends with .ccf.

Upvotes: 137

Related Questions