Is there a way how to write a JavaScript regular expression that would recognize .ts file extension for this:
"file.ts"
but would fail on this:
"file.vue.ts"
What I need is, if the file name ends with .vue.ts, it shouldn't be handled as a .ts file.
I've tried a lot of things with no success.
Update: It needs to be a regular expression, because that's what I'm passing to a parameter of a function.
Is there a way how to write a JavaScript regular expression that would recognize .ts file extension for this:
"file.ts"
but would fail on this:
"file.vue.ts"
What I need is, if the file name ends with .vue.ts, it shouldn't be handled as a .ts file.
I've tried a lot of things with no success.
Update: It needs to be a regular expression, because that's what I'm passing to a parameter of a function.
Share Improve this question edited Mar 22, 2018 at 18:04 Tom Shane asked Mar 22, 2018 at 17:47 Tom ShaneTom Shane 7047 silver badges18 bronze badges 8- has it to be a regular expression? – Nina Scholz Commented Mar 22, 2018 at 17:48
- /(.*).ts$/ this should check that the last 3 chars are '.ts' – PRDeving Commented Mar 22, 2018 at 17:49
- Possible duplicate of Regex JavaScript image file extension – Scott Weaver Commented Mar 22, 2018 at 17:50
- Possible duplicate of Javascript regex for matching/extracting file extension – Aman B Commented Mar 22, 2018 at 17:50
- @NinaScholz Yes, I need it to be a regular expression. – Tom Shane Commented Mar 22, 2018 at 17:51
4 Answers
Reset to default 3Regex for that is ^[^.]+.ts$
var x=/^[^.]+.ts$/;
console.log(x.test("file.ts"));
console.log(x.test("file.vue.ts"));
console.log(x.test("file.vue.ts1"));
Explanation:-
^[^.]+.ts$
^ ---> start of line
[^.]+ ---> match anything which is not '.' (match atleast one character)
^[^.]+ ---> match character until first '.' encounter
.ts ---> match '.ts'
$ ---> end of line
.ts$ ---> string end with '.ts'
You could look for a previous ing dot and if not return true
.
console.log(["file.ts", "file.vue.ts"].map(s => /^[^.]+\.ts$/.test(s)));
This will work except for special characters. Will allow for uppercase letters, lowercase letters, numbers, underscores, and dashes:
^[a-zA-Z0-9\_\-]+(\.ts)$
const regex = /(.*[^.vue]).ts/g;
abc.ts.ts Matches
abc.xyx.htm.ts Matches
abc.vue.ts Fails
xyz.abx.sxc.vue.ts Fails
Javascript regex should be this one.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742298845a4417659.html
评论列表(0条)