Home > Enterprise >  Finding something in between characters
Finding something in between characters

Time:09-23

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

CodePudding user response:

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.

CodePudding user response:

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))

CodePudding user response:

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