I have the following string:
documentation/:docsID/items
Now I want to replace the :docsID
by a variable.
So if the variable is equal to 12
, i want the link look like documentation/12/items
.
How can I accomplish this with Javascript?
Asked
Active
Viewed 1,278 times
-4

Julian Declercq
- 1,536
- 3
- 17
- 32

Inbar Azulay
- 247
- 1
- 4
- 25
-
You can learn some elementary [RegExp](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp). – Mitya Nov 22 '16 at 12:19
-
1`.replace(':docsID', varName)` ??? – Washington Guedes Nov 22 '16 at 12:19
-
1I always think SO is undermined when people post answers to questions that show no search effort. What's the point in people to do their research when they can do none and get answers anyway? – Mitya Nov 22 '16 at 12:20
-
1replace if it isn't a hardcoded string else use `"asdf"+12+"fdsa"` – Kevin Kloet Nov 22 '16 at 12:21
-
this is a valid question. i think he just didn't state clearly what he was asking. this question should be pointed to https://stackoverflow.com/questions/3304014/how-to-interpolate-variables-in-strings-in-javascript-without-concatenation?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – MartianMartian May 01 '18 at 12:03
2 Answers
0
try this
var str="documentation/:docsID/items"
var val=12;
str =str.replace(":docsID",val)

Ramniwas chhimpa
- 205
- 1
- 2
- 10