I have the following string:
Provided you always have the string you want to get between a
/d/ and an
/edit, you can capture it with this regex:
(.*\/d\/)(.*)(\/edit.*); it should be the second group.
Below is an illustration of how you can get it:
var data = "/presentation/d/1JHjE6pWJRujtMQaXcL92HBD8ZKq35au8moEv_zP2D_-0/edit"; my_preferred_string = data.match(/(.*\/d\/)(.*)(\/edit.*)/); console.log(my_preferred_string);
Explaining the regex,
Here, we have three groups; the groups are confined within brackets as in:
(the group elements), where the group elements represents the desired matches.
The 1st group is captured with
In the expression for that group,
.* is used to capture any character with the exception of line terminators. In that same portion, we've made use of the quantifier
* for matches between zero and unlimited times, as many times as possible. Then, the
\/ as a match for the
/ character (case sensitive), where the preceding
\ is an escape, followed by the letter
d (case sensitive, to match specifically d as a character), and ending with
\/, just as before the
The 2nd group which is the actual portion we are interested in is captured with the
.* is just as explained above in capturing the preceding expression.
The 3rd group is captured with
.* are equally as explained above (group 1), and the
edit as a character match for edit (case sensitive).
g is a flag (Global pattern flags). It help us get all matches and does not return after we got the first match of our regex.