-4

I have a string url as follows:

var url = '/test/mybin/processes/edit.jsp?id={processId}';

I want to extract the string "processId" from above url. I can get it using indexOf and substring methods, but is there a better alternative to do it? Can we do it using Regex?

rene
  • 39,748
  • 78
  • 111
  • 142
  • 1
    for such a simple string deconstruction, use string operations. regex is massive overkill. – Marc B Sep 16 '14 at 18:30
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Waxen Sep 16 '14 at 18:44
  • Thanks a lot, everyone. Most of the sugeestions below work. ,But I would like to know which is the best option from performance aspect? Using Regex or match or indexOf/substring? – user3792795 Sep 16 '14 at 18:46
  • Why the concern about performance? Is that an operation your application is going to repeat gazillions of times in a short period? – Alexis Pigeon Sep 27 '14 at 15:52

4 Answers4

1
var procid = url.split("id=")[1];
BReal14
  • 1,583
  • 1
  • 11
  • 34
0

JavaScript strings has regex support embeded:

var processId = url.match(/\?id=(.+)/)[1];

Only thing U need - be familiar with regular expressions

And if braces are problem:

var processId = url.match(/\?id=\{(.+)\}/)[1];
comdiv
  • 775
  • 4
  • 22
0
'/test/mybin/processes/edit.jsp?id={processId}'.split('{')[1].split('}')[0]
sabithpocker
  • 14,744
  • 1
  • 38
  • 71
0

You can easily use a regex:

url.match(/\{([^}]*)\}/)[1];

But for this simple pattern, using indexOf and substring, while not as terse, will have much better performance. TIMTOWTDI

Ted Hopp
  • 227,407
  • 48
  • 383
  • 507