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)
Related
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]);
}
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.';
This question already has answers here:
Remove consecutive duplicate characters in a string javascript
(5 answers)
Closed 2 years ago.
I want to delete all the repeat letter in a string with javascript.
mot = "message in a bottle";
mot = mot.replace(/[\w]{2,}/i, '');
console.log(mot)
// result wish : "mesage in a botle"`
Thank you for your help.
You can group the word characters and then use a backreference to check if the same character follows that grouped character (with \1). You can replace all found occurrences with the group using $1. Ensure that you use the global flag (/g) to match all occurrences:
const mot = "message in a bottle";
const res = mot.replace(/(\w)\1/ig, '$1');
console.log(res);
This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Closed 2 years ago.
I saw this Regex replace `a.b` to `a. b`? I don't understand it's for Python not for Javascript or it isn't well explained.
I know how to split "a.b" to ["a" "." "b"] with
regex = /(\.)/;
test = "a.b";
results = test.split(regex);
I can't see what regex to get
["a" ".b"]
You could split by the positive lookahead of a dot.
var string = "a.b",
result = string.split(/(?=\.)/);
console.log(result);
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Replace all plus signs (+) with space in a string
(1 answer)
Closed 5 years ago.
How can I replace multiple (++++++) signs with blank space?
Example: var string = "+++++++++++++++++";
var string = "+++++++++++++++++";
string = string.replace(/\+*/g, '');
console.log(string.length);
const str = 'abc++++++def++++frberbf++fsvsf';
const newstr = str.replace(/(.)\+{2,}/g, ' ');
console.log(newstr);