get only 5868 form url http://localhost:52717/ticketwizard/eventfeed/0/5868?keywordsearchstring=eventkeyword in javascript

One way to extract the number 5868 from the URL is to use Regular Expression and String manipulation. The following code shows an example of how to accomplish this in JavaScript:

index.tsx
const url = "http://localhost:52717/ticketwizard/eventfeed/0/5868?keywordsearchstring=eventkeyword";

// Use a Regular Expression to extract the number from the URL
const match = url.match(/\/(\d+)\?/);

// Extract the captured number from the match array
const number = match && match[1];

console.log(number); // Output: "5868"
330 chars
10 lines

The regular expression /\/(\d+)\?/ matches the first occurrence of a forward slash, followed by one or more digits (captured as a group), followed by a question mark. The parentheses in the regular expression denote a capturing group, which we can access later via the match array.

We then use the logical AND && operator to check if the match array exists (to avoid a TypeError if the regular expression doesn't match anything), and access the captured number via index 1.

Finally, we log the extracted number to the console.

gistlibby LogSnag