Finding something in between characters

Viewed 29

How can I get the word in between quotation marks and then replace it with something else from reading from a file

Note: I know how to read and write from files, I just need to know about getting the word from inside quotation marks

3 Answers

If you know the word you're looking for:

let text = 'Sentence with "word" in it';
let wordFromFile = "nothing";
let newText = text.replace('"word"', wordFromFile);

Or else regular expression is useful.

Using Regex(regular expression) with regex replace :

let str = 'How can I get the word in between "quotation marks"';
let word = str.replace(/.*"(.*?)".*/g,'$1');
let replacedWord = 'single quotes';
console.log(str.replace(word,replacedWord))

You can find the word with a quotation mark for example word by /"[^"]+"/ and replace it with other words for example key

const str = 'This is the "word" which inside two quotation marks'

const result = str.replace(/"[^"]+"/g, 'key')

console.log(result)

Related