0

I'm trying to run a function after a certain ajax request is completed. Since there are multiple requests being made across the site, I'm filtering the one I want with settings.url like in the official documentation:

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "ajax/test.html" ) {
    $( ".log" ).text( "Triggered ajaxComplete handler. The result is " +
      xhr.responseText );
  }
});

The actual url looks something like this https://example.com/page?ids=%2C1%2C2%2C Everything after ?ids= is changing from request to request.

Now, the above code only fires if the settings.url is the exact same as in the request, id's included.

if ( settings.url === "https://example.com/page?ids=%2C1%2C2%2C" )

So I need to match settings.url with https://example.com/page?ids= and a wildcard at the end, since the id's are not important.

Would this be possible by using regex?

Ivar
  • 6,138
  • 12
  • 49
  • 61
Bonovski
  • 35
  • 1
  • 7
  • 1
    `if ( settings.url.indexOf("https://example.com/page?ids") === 0) ...` – mplungjan Feb 10 '20 at 09:48
  • Regular expressions are the normal method of matching patterns in strings. Why wouldn't it be possible? What happened when you tried? If it didn't work, you probably just made a mistake (remember that you have to escape `/` and `?` in regular expressions). You need to post your attempt if you want help with it. – Barmar Feb 10 '20 at 09:49

2 Answers2

0

If you want to do it with regex you could take Regex to parse an URL (What is a good regular expression to match a URL?) and finish it off with your delimiter, in your case the =.

(www|http:|https:)+[^\s]+[\w]=
Envious
  • 3
  • 1
  • 2
0
let regex = /[^ ]*pageids=[^ ]*/;

if (settings.url.match(regex)) {
  console.log('triggered');
  console.log(settings.url);
}

It's working now. I forgot to replace settings.url=== with settings.url.match().

Bonovski
  • 35
  • 1
  • 7