A
A
AndreyKiyah2020-01-14 14:14:25
JavaScript
AndreyKiyah, 2020-01-14 14:14:25

How to parse URL correctly?

We have a link -->

<a [routerLink]="['/dashboard/micebot/?a=serv11111121']">/micebot</a>

In general, the essence is that you need to check the url, whether there is an "a" after the question mark and whether it is equal to "a".
12 characters in which small Latin letters and numbers from 1 to 5.
If not, then redirect

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2020-01-14
@twobomb

if("&lt;a [routerLink]=\"['/dashboard/micebot/?a=serv11111121']\"&gt;/micebot&lt;/a&gt;".search(/\?a=[a-z1-5]{12}[^a-z1-5]/) == -1){
  window.location ="redirect url";
}

D
dollar, 2020-01-14
@dollar

It's better to separate the part with the parameters, and then break it into parts like key=value.
It will be a universal solution.
It would be easier to do exactly what you asked: check for "a" immediately after the question mark:

var text = `<a [routerLink]="['/dashboard/micebot/?a=serv11111121']">/micebot</a>`;
var m = text.match(/\?a=([a-z1-5]*)/);
if (m) {
  let a = m[1];
  console.log('"a" существует и равно ', a);
  if (a.length == 12) console.log('Длина "a" ровно 12, не больше');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question