I have a string like this:
let string = "/gb/fr/firstPage/secondPage
.\
I want to compare the /gb/fr/
part against an array like below:
let array = [ "/fr/fr", "/de/de", "/es/es","/ro/ro", "/it/it"]
and if its the same return the string, but if not the same return :
string = "/gb/en/firstPage/secondPage
\
Can you help me out please or explain how to do it ?
more example:
"/ee/et/firstPage/secondPage"
should return :
"/ee/en/firstPage/secondPage"`
another example :
"/lt/lt/firstPage/secondPage"
should return:
"/lt/en/firstPage/secondPage"
so it basically checks the first part /fr/
if it exists it will return the corresponding link otherwise it will replace the second part with `/en/`
CodePudding user response:
To compare the first part of the string against an array of strings, you can use the startsWith method to check if the string starts with any of the values in the array. If the string does not start with one of the values in the array, you can use the replace method to replace the first part of the string with the desired value.
Here is an example of how you could do this:
let string = "/gb/fr/firstPage/secondPage";
let array = [ "/fr/fr", "/de/de", "/es/es","/ro/ro", "/it/it"];
// check if the string starts with any of the values in the array
let isMatch = array.some(a => string.startsWith(a));
if (!isMatch) {
// replace the first part of the string with "/gb/en"
string = string.replace(/^\/\w \/\w /, "/gb/en");
}
console.log(string); // "/gb/en/firstPage/secondPage"
In this code, the some method is used to check if any of the values in the array match the start of the string. If none of the values match, the replace method is used to replace the first part of the string with the desired value. This method uses a regular expression to match the first two / characters, followed by one or more word characters (\w ), followed by another / character and one or more word characters. This regular expression will match a string like /gb/fr or /us/en, but not a string like /gb or /fr/fr/fr.
Finally, the updated string is logged to the console.
-chatgpt
CodePudding user response:
If I understand correctly, taking input "/<A>/<B>/<C>/<D>"
, you'd like to replace <B>
with en
in case <A>
does not match any of the array item's first word, e.g. fr
, de
, es
, ro
, or it
.
You can build a regex with a negative lookahead, e.g. the regex matches only if the lookahead condition is not met:
const input = [
"/fr/fr/firstPage/secondPage",
"/gb/fr/firstPage/secondPage",
"/ee/et/firstPage/secondPage",
"/lt/lt/firstPage/secondPage"
];
const array = ["/fr/fr", "/de/de", "/es/es","/ro/ro", "/it/it"];
let regex = new RegExp('^\\/(?!(?:' array.map(s => s.substring(1, 3)).join('|') '))(..)\\/..');
// resulting regex: /^\/(?!(?:fr|de|es|ro|it))(..)\/../
console.log('regex: ' regex.toString());
input.forEach(str => {
let result = str.replace(regex, '/$1/en');
console.log(str ' => ' result);
});
Output:
regex: /^\/(?!(?:fr|de|es|ro|it))(..)\/../
/fr/fr/firstPage/secondPage => /fr/fr/firstPage/secondPage
/gb/fr/firstPage/secondPage => /gb/en/firstPage/secondPage
/ee/et/firstPage/secondPage => /ee/en/firstPage/secondPage
/lt/lt/firstPage/secondPage => /lt/en/firstPage/secondPage
Explanation of new RegExp()
:
- code
new RegExp('^\\/(?!(?:' array.map(s => s.substring(1, 3)).join('|') '))(..)\\/..')
results in regex/^\/(?!(?:fr|de|es|ro|it))(..)\/../
with your given array
Explanation of resulting regex /^\/(?!(?:fr|de|es|ro|it))(..)\/../
:
^
-- anchor at start of string\/
-- literal/
(?!
-- start of negative lookahead(?:fr|de|es|ro|it)
-- non-capture group containing a list of items with logical OR|
)
-- end of negative lookahead(..)
-- capture group 1 for two chars\/..
-- literal/..
Explanation of replace '/$1/en'
:
/
-- literal/
$1
-- capture group 1 value/en
-- literal/en