I have a string that looks like this:
'"a": "...",
"b": "...",
"c": "...",
"d": "...",
"text": "Part to access and modify",
"f": "...",
"g": "..."'
And i would like to access to the text after '"text":'
.
The way I do it now is const text = str.split('"text": ')[1].split('"')[1]
and thanks to that I can access and modify my text.
I don’t know if there is a more efficient method but my biggest problem is to succeed in replacing the old text with the new text in the basic structure.
How can i do it please ?
Before:
'"a": "...",
"b": "...",
"c": "...",
"d": "...",
"text": "Part to access and modify",
"f": "...",
"g": "..."'
After:
'"a": "...",
"b": "...",
"c": "...",
"d": "...",
"text": "Modified text",
"f": "...",
"g": "..."'
2
Answers
You can use a lookbehind assertion regex:
It also looks like a part of an object literal, so:
This looks like JSON content and it would be best/safest to use a parser, rather than regex. That being said, you could use regex here as follows: