JavaScript JavaScript: How to pull out a string from a URL using a regular expression javascript string pull,js retrieve string,javascript pull query string,

In java, I have this URL as a string:

window.location.href = 
"http://localhost:8080/bladdey/shop/c6c8262a-bfd0-4ea3-aa6e-d466a28f875/hired-3";

I want to create a javascript regular expression to pull out the following string:

c6c8262a-bfd0-4ea3-aa6e-d466a28f875

To find left hand marker for the text, I could use the regex:

window\.location\.href \= \"http\:\/\/localhost:8080\/bladdey\/shop\/

However, I don't know how to get to the text between that and /hired3"

What is the best way to pull out that string from a URL using javascript?

Answer:1

You could split the string in tokens and look for a string that has 4 occurrences of -.

Or, if the base is always the same, you could use the following code:

String myString = window.location.href;
myString = myString.substring("http://localhost:8080/bladdey/shop/".Length());
myString = myString.subString(0, myString.indexOf('/'));
Answer:2

Use a lookahead and a lookbehind,

(?<=http://localhost:8080/bladdey/shop/).+?(?=/hired3)

Check here for more information.

Also, there is no need to escape the : or / characters.

Answer:3

You need a regex, and some way to use it...

  String theLocation = "http://localhost:8080/bladdey/shop/c6c8262a-bfd0-4ea3-aa6e-d466a28f8752/hired-3";
  String pattern = "(?</bladdey/shop/).+?(?=/hired3)";

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  if (m.find( )) {
     System.out.println("Found value: " + m.group(0) );
  } else {
     System.out.println("NO MATCH");
  }

note - this will still work when you change the host (it only looks for bladdey/shop/)

Answer:4

I'm running the following code to set a limit on characters in a textarea: (function($){ $.fn.extend({ limit: function(limit,element) { var interval, f; var self = $(this); ...

I'm running the following code to set a limit on characters in a textarea: (function($){ $.fn.extend({ limit: function(limit,element) { var interval, f; var self = $(this); ...

  1. count carriage return javascript
  2. count carriage return excel
  3. count carriage return c#
  4. count carriage return java
  5. count carriage return python
  6. excel count carriage returns in cell
  7. javascript count carriage returns in string
  8. vba count carriage returns in string
  9. c# count carriage returns in a string
  10. sql count carriage returns
  11. jquery count carriage return textarea
  12. count lines carriage return
  13. count number of carriage returns excel
  14. vb.net count carriage returns in string
  15. count number of carriage returns vba
  16. php count carriage return
  17. js count carriage returns

I know how to click a link using javascript, document.getElementById('yourLinkID').click(); But what if your ID is always changing or you are not sure what your ID is going to be? I have a html ...

I know how to click a link using javascript, document.getElementById('yourLinkID').click(); But what if your ID is always changing or you are not sure what your ID is going to be? I have a html ...

  1. click save html

I have two javascript objects that are used to replicate data objects. They are filled in via onclick events, and I want to clear them out after a save event. For example, var currentStrategy = { ...

I have two javascript objects that are used to replicate data objects. They are filled in via onclick events, and I want to clear them out after a save event. For example, var currentStrategy = { ...

  1. clear internal javascript

I need to use a simple node_module inside a simple typescript file, but it seems that the compiler doesn't want to get it. Here's my simple ts file : import glob = require('glob'); console.log(glob);...

I need to use a simple node_module inside a simple typescript file, but it seems that the compiler doesn't want to get it. Here's my simple ts file : import glob = require('glob'); console.log(glob);...

  1. typescript module require
  2. typescript requirejs module
  3. typescript require module not found
  4. typescript import require module
  5. typescript cannot find module require
  6. cannot find module 'typescript' require stack