what is the regex for the following part? [duplicate] - javascript

This question already has answers here:
Get Filename from URL and Strip File Extension
(5 answers)
Closed 1 year ago.
src/insp/custom/extractThis1.q.ts
what would be the regex for extractThis1 ?
I have tried like this /\/(.*).q.ts/gm but didn't work

You can match until the last occurrence of /, and then use a capture group to match any char except a . followed by .q.ts
^.*\/([^.]+)\.q\.ts$
Regex demo
const s = "src/insp/custom/extractThis1.q.ts";
const regex = /^.*\/([^.]+)\.q\.ts$/;
const m = s.match(regex);
if (m) {
console.log(m[1]);
}

Related

Replace Space with Dash and Remove Semicolon [duplicate]

This question already has answers here:
Replace comma or whitespace with hyphen in same string
(3 answers)
Closed 2 years ago.
This regex replaces empty spaces with a dash replace(/\s+/g, "-").
If I also want to remove any semicolons, how do I go about adding that to the above regex?
Basically, I want this string hello; how are you to be hello-how-are-you
You could add ; in [] - which means groups and ranges
/[\s;]+/g
const str = "hello; how are you"
const res = str.replace(/[\s;]+/g, "-")
console.log(res)

Javascript - Replace substring if the substring is not prepended with a character Regex or other [duplicate]

This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
How to make word boundary \b not match on dashes
(3 answers)
Closed 2 years ago.
I need to replace all instances of a string(needle) within another string(haystack), but only where the needle is not preceeded by a specific character, in this case '-'
var needle = 'secret';
var haystack = 'secret title date user -secret secret something.secret';
//Regex that needs to be adjusted
var result = haystack.replace(/secret/g, '');
//Desired result is
var desiredResult = ' title date user -secret something.';

Why regex do not get all matches? [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
RegEx to extract all matches from string using RegExp.exec
(19 answers)
Closed 2 years ago.
Regex link is https://regex101.com/r/xAQoU0/4. There it finds two matches.
const regex = /\$\$\[([(a-z-A-Z.)]*)]/;
const str = "<p>I have $$[worksAmount] works and $$[reviewsAmount] reviews</p>";
const match = regex.exec(str);
console.log(match)
But, when I run this snippet it logs in console only first match. So how to get all matches?
const regex = /\$\$\[([(a-z-A-Z.)]*)]/g;
const str = "<p>I have $$[worksAmount] works and $$[reviewsAmount] reviews</p>";
const match = str.match(regex);
console.log(match)

Prevent special characters in string [duplicate]

This question already has answers here:
Regular expression for excluding special characters [closed]
(11 answers)
Closed 4 years ago.
I have this regular expression and I need to prevent a password having any of these symbols: !##$%^&*()_, it is working for !##$% but not for this password !##$%D.
$scope.matchPatternPassword = new RegExp("[^!##$%^&*()_]$");
Your regex was only checking for any of those symbols at the end of the string, that's why the one ending in a letter was working.
The regex should be:
$scope.matchPatternPassword = /^[^!##$%^&*()_]+$/;
This matches any string that doesn't have any of those characters.
Here's a working example: https://regexr.com/3nh9f
const regex = /^[^!##$%^&*()_]+$/;
const passwords = [
'!##$%',
'!##$%D',
'yes',
'valid-password',
'im-valid-too',
'invalid$',
'super!-invalid',
'mypassword'
];
console.log(passwords.filter(password => regex.test(password)));

How do I replace all instances of a dash (-) with a foward slash (/) in a string [duplicate]

This question already has answers here:
Converting date string from dashes to forward slashes
(4 answers)
Closed 8 years ago.
I need your help,
How do I go about replacing all instances of a dash - with a forward slash / in a string
var x = "03-04-2014"
After conversion:
var x = "03/04/2014"
with string.replace
x = x.replace(/\-/g, '/')
or split
x.split('-').join('/')

Categories

Resources