How do I url encode content in a string that is between quotes [closed]
I want to turn the string a b "c d" e f
into a b c%20d e f
in JavaScript. I assume it has something to do with RegEx, but I don't really understand it.
EDIT: I need it to work multiple times in a string, eg a b "c d" e f "g h" i j
turns into a b c%20d e f g%20h i j
javascript regex encodeuricomponent
closed as too broad by Wiktor Stribiżew, Bharata, Rob, rickvdbosch, Tomasz Mularczyk Jan 4 at 7:35
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I want to turn the string a b "c d" e f
into a b c%20d e f
in JavaScript. I assume it has something to do with RegEx, but I don't really understand it.
EDIT: I need it to work multiple times in a string, eg a b "c d" e f "g h" i j
turns into a b c%20d e f g%20h i j
javascript regex encodeuricomponent
closed as too broad by Wiktor Stribiżew, Bharata, Rob, rickvdbosch, Tomasz Mularczyk Jan 4 at 7:35
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
You haveencodeuricomponent
as one of your tags. Why not use that?
– Taplar
Jan 3 at 18:31
What doesa b "c d" e " f g
turn into ?
– sln
Jan 3 at 19:55
@Taplar I was aware it would use encodeuricomponent, but I was unsure how I would implement this with RegEx. I would not have known how to do the accepted answer.
– Archie Baer
Jan 3 at 21:23
add a comment |
I want to turn the string a b "c d" e f
into a b c%20d e f
in JavaScript. I assume it has something to do with RegEx, but I don't really understand it.
EDIT: I need it to work multiple times in a string, eg a b "c d" e f "g h" i j
turns into a b c%20d e f g%20h i j
javascript regex encodeuricomponent
I want to turn the string a b "c d" e f
into a b c%20d e f
in JavaScript. I assume it has something to do with RegEx, but I don't really understand it.
EDIT: I need it to work multiple times in a string, eg a b "c d" e f "g h" i j
turns into a b c%20d e f g%20h i j
javascript regex encodeuricomponent
javascript regex encodeuricomponent
edited Jan 3 at 21:27
Archie Baer
asked Jan 3 at 18:28
Archie BaerArchie Baer
119114
119114
closed as too broad by Wiktor Stribiżew, Bharata, Rob, rickvdbosch, Tomasz Mularczyk Jan 4 at 7:35
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Wiktor Stribiżew, Bharata, Rob, rickvdbosch, Tomasz Mularczyk Jan 4 at 7:35
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
You haveencodeuricomponent
as one of your tags. Why not use that?
– Taplar
Jan 3 at 18:31
What doesa b "c d" e " f g
turn into ?
– sln
Jan 3 at 19:55
@Taplar I was aware it would use encodeuricomponent, but I was unsure how I would implement this with RegEx. I would not have known how to do the accepted answer.
– Archie Baer
Jan 3 at 21:23
add a comment |
1
You haveencodeuricomponent
as one of your tags. Why not use that?
– Taplar
Jan 3 at 18:31
What doesa b "c d" e " f g
turn into ?
– sln
Jan 3 at 19:55
@Taplar I was aware it would use encodeuricomponent, but I was unsure how I would implement this with RegEx. I would not have known how to do the accepted answer.
– Archie Baer
Jan 3 at 21:23
1
1
You have
encodeuricomponent
as one of your tags. Why not use that?– Taplar
Jan 3 at 18:31
You have
encodeuricomponent
as one of your tags. Why not use that?– Taplar
Jan 3 at 18:31
What does
a b "c d" e " f g
turn into ?– sln
Jan 3 at 19:55
What does
a b "c d" e " f g
turn into ?– sln
Jan 3 at 19:55
@Taplar I was aware it would use encodeuricomponent, but I was unsure how I would implement this with RegEx. I would not have known how to do the accepted answer.
– Archie Baer
Jan 3 at 21:23
@Taplar I was aware it would use encodeuricomponent, but I was unsure how I would implement this with RegEx. I would not have known how to do the accepted answer.
– Archie Baer
Jan 3 at 21:23
add a comment |
1 Answer
1
active
oldest
votes
If you have just one embedded string:
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
For multiple embedded strings:
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
You'll probably want tomake that a non-greedy match"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.
– a p
Jan 3 at 18:33
1
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
1
I'd suggests.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and[^"]+
should be a bit more efficient than.+?
).
– Jeto
Jan 3 at 18:37
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of[^"]+?
over.+?
– ic3b3rg
Jan 3 at 18:41
@ic3b3rg It's[^"]+
not[^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!
– Jeto
Jan 3 at 18:43
|
show 5 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have just one embedded string:
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
For multiple embedded strings:
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
You'll probably want tomake that a non-greedy match"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.
– a p
Jan 3 at 18:33
1
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
1
I'd suggests.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and[^"]+
should be a bit more efficient than.+?
).
– Jeto
Jan 3 at 18:37
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of[^"]+?
over.+?
– ic3b3rg
Jan 3 at 18:41
@ic3b3rg It's[^"]+
not[^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!
– Jeto
Jan 3 at 18:43
|
show 5 more comments
If you have just one embedded string:
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
For multiple embedded strings:
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
You'll probably want tomake that a non-greedy match"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.
– a p
Jan 3 at 18:33
1
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
1
I'd suggests.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and[^"]+
should be a bit more efficient than.+?
).
– Jeto
Jan 3 at 18:37
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of[^"]+?
over.+?
– ic3b3rg
Jan 3 at 18:41
@ic3b3rg It's[^"]+
not[^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!
– Jeto
Jan 3 at 18:43
|
show 5 more comments
If you have just one embedded string:
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
For multiple embedded strings:
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
If you have just one embedded string:
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
For multiple embedded strings:
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
const s = 'a b "c d" e f'
console.log(s.replace(/"(.+)"/, (s, t) => encodeURI(t)))
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
const s = 'a b "c d" e "f g" h'
console.log(s.replace(/"(.+?)"/g, (s, t) => encodeURI(t)))
edited Jan 3 at 21:30
answered Jan 3 at 18:31
ic3b3rgic3b3rg
10.9k42045
10.9k42045
You'll probably want tomake that a non-greedy match"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.
– a p
Jan 3 at 18:33
1
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
1
I'd suggests.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and[^"]+
should be a bit more efficient than.+?
).
– Jeto
Jan 3 at 18:37
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of[^"]+?
over.+?
– ic3b3rg
Jan 3 at 18:41
@ic3b3rg It's[^"]+
not[^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!
– Jeto
Jan 3 at 18:43
|
show 5 more comments
You'll probably want tomake that a non-greedy match"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.
– a p
Jan 3 at 18:33
1
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
1
I'd suggests.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and[^"]+
should be a bit more efficient than.+?
).
– Jeto
Jan 3 at 18:37
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of[^"]+?
over.+?
– ic3b3rg
Jan 3 at 18:41
@ic3b3rg It's[^"]+
not[^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!
– Jeto
Jan 3 at 18:43
You'll probably want tomake that a non-greedy match
"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.– a p
Jan 3 at 18:33
You'll probably want tomake that a non-greedy match
"(.+?)"
so that if you encounter two pair of quotes, you don't get everything between the first and fourth URLEncoded.– a p
Jan 3 at 18:33
1
1
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
That's partly why I included the caveat - the requirements aren't that clear
– ic3b3rg
Jan 3 at 18:34
1
1
I'd suggest
s.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and [^"]+
should be a bit more efficient than .+?
).– Jeto
Jan 3 at 18:37
I'd suggest
s.replace(/"([^"]+)"/g, (s, t) => encodeURI(t))
(global flag because the question's title seems to imply so, and [^"]+
should be a bit more efficient than .+?
).– Jeto
Jan 3 at 18:37
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of
[^"]+?
over .+?
– ic3b3rg
Jan 3 at 18:41
@Jeto That's valid if the requirements are to handle multiple embedded strings, however it's not specified by OP. Also, there's no advantage of
[^"]+?
over .+?
– ic3b3rg
Jan 3 at 18:41
@ic3b3rg It's
[^"]+
not [^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!– Jeto
Jan 3 at 18:43
@ic3b3rg It's
[^"]+
not [^"]+?
, always better when you can omit the non-greedy operator, as far as I know. As for the rest, I find it more likely that he wants it anywhere within the string, but he can easily adjust it if needed. Just nitpicking anyway!– Jeto
Jan 3 at 18:43
|
show 5 more comments
1
You have
encodeuricomponent
as one of your tags. Why not use that?– Taplar
Jan 3 at 18:31
What does
a b "c d" e " f g
turn into ?– sln
Jan 3 at 19:55
@Taplar I was aware it would use encodeuricomponent, but I was unsure how I would implement this with RegEx. I would not have known how to do the accepted answer.
– Archie Baer
Jan 3 at 21:23