Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save namikazebadri/360308c7b9ff403c9468ba938fca65a0 to your computer and use it in GitHub Desktop.

Select an option

Save namikazebadri/360308c7b9ff403c9468ba938fca65a0 to your computer and use it in GitHub Desktop.
This script originally an attempt to test if path is active or not in react app, the case is about similar pat like /finance/invoice and /finance/invoice_landlord.
const path = '/finance/invoice';
const pathCorrects = [
path,
path + '/',
path + '?',
path + '#name',
path + '#name?',
path + '#name?first_name=unis&last_name=badri',
path + '/#name?first_name=unis&last_name=badri',
path + '?first_name=unis&last_name=badri',
path + '/?first_name=unis&last_name=badri',
]
const pathWrongs = [
path + '_landlord',
path + '_landlord/',
path + '_landlord?',
path + '_landlord#name',
path + '_landlord#name?',
path + '_landlord#name?first_name=unis&last_name=badri',
path + '_landlord/#name?first_name=unis&last_name=badri',
path + '_landlord?first_name=unis&last_name=badri',
path + '_landlord/?first_name=unis&last_name=badri',
]
const regexExpression = '(?!\\_)(\\?|\\/|\\#|)(.*?)$';
console.log('Correct conditions: ')
for(let i = 0; i < pathCorrects.length; i++) {
const regex = new RegExp(path + regexExpression, 'g');
console.log(regex.test(pathCorrects[i]));
}
console.log('Wrong conditions: ')
for(let i = 0; i < pathWrongs.length; i++) {
const regex = new RegExp(path + regexExpression, 'g');
console.log(regex.test(pathWrongs[i]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment