There is a line that I get by accessing document.location.pathname - / airlines / su / .

What do I need to do with this line? Initially, you need to check if the line contains something other than /airlines/ . I wanted to do this with a regular expression that checks if the line is suitable for /airlines/NN/ , but I don’t know how to do it. There is an option to take the length of the string /airlines/ and compare it with the fact that in document.location.path , but it will work under the condition if you write anything in the address bar. Therefore, this option is not suitable. But besides the fact that I need to check this line, I also need to cut out two characters: /airlines/ NN / . And finally, you need to check whether these two characters are NN alphanumeric and they must contain strictly two characters. Options that fit: s7, su, ps, a1. Is there some elegant way to do this in a couple of lines? And then my description looks too cumbersome.

  • and what prevented you from writing this regular expression? ^\/airlines\/([az\d]{2})\/$ - teran

1 answer 1

 var strings = [ '/airlines/su/', '/airlines/s1/', '/airlines/22/', '/airlines/sup/', '/airlines/s/' ]; for (var i = 0; i < strings.length; i++) { var match = strings[i].match(/\/airlines\/([a-z0-9]{2})\//i); if (match) console.log('Str: ' + strings[i] + ', code: ' + match[1]); else console.log('Str: ' + strings[i] + ', code: Not found'); }; 

  • something seems to me that 22 need to be deleted - teran
  • @teran author will say - exclude. Now he has два символа NN буквенно-числовыми и содержать они должны строго два символа. - Anton Shchyrov
  • @teran is not, since 22 everything is fine, this value can also be. - JamesJGoodwin
  • Thanks for the decision! - JamesJGoodwin
  • @JamesJGoodwin If my answer helped you, mark it as correct - Anton Shchyrov