-2

Here is example of my string: "https://dev.testapi.com/api/v1/properties?id:xyz&surname:kronenberg&active=true". Question is: i want to save the last value of my string, in this case its "active:".

Another examples of the string:

var url =  "https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&property_type:true";
var url =  "https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&active:true";
var url =  "https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&user_id:true";

= "https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&property_type:true"

2 Answers2

0

var url1 = '"https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&property_type:true"',
    url2 = '"https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&active:true"',
    url3 = '"https://dev.testapi.com/api/v1/properties?id=xyz&surname:kronenberg&user_id:true"',
    url4 = '"dev.testapi.com/api/v1/properties?id=xyz"';

    console.log(url1.match(/(?!.+[&?])(\w+)/g)[0]);
    console.log(url2.match(/(?!.+[&?])(\w+)/g)[0]);
    console.log(url3.match(/(?!.+[&?])(\w+)/g)[0]);
    console.log(url4.match(/(?!.+[&?])(\w+)/g)[0]);
kind user
  • 32,209
  • 6
  • 49
  • 63
0

See my demo in action here Regex101 Demo.

(\w+)(?:(?:=|:)\w+)?$ is the required regex in Javascript.

(\w+) Sets a capturing group.

?: When inside a capturing group, denote a non-capturing group.

$ denotes end of the statement.

Community
  • 1
  • 1
Himanshu
  • 447
  • 1
  • 8
  • 17