Skip to content

User input always surrounded by double quotations in concatenated URL

I am developing a Chrome extension that, among other things, allows you to right-click selected text and search for that selected text in a new tab after it’s tested against some regular expression patterns.

In the below JavaScript code, the selected text is made into a string, then encoded as a component of a URI, then it is supposed to remove any double quotations from the beginning or end of the variable, but the double quotations are inexorably still in the URL of the newly created tab. I can’t figure out what is causing the quotations to show up in the end result in the first place, let alone why they are there even after the .replace method is used. I need there to be no quotations at all and only pass in the selected text into the URL. Please help!

var EncodedSelectedText = encodeURIComponent(JSON.stringify(info.selectionText));
var NoQuotes = EncodedSelectedText.replace(/^"|"$/g, '');

if (RITMRegExPattern.test(info.selectionText)) {
        url: ' 
}else if . . .

Resulting URL of the newly created Chrome tab looks like this: 

but needs to look like this (without the quotations surrounding abc1234567): 


Remove JSON.stringify and reload your extension. – wOxxOm

This fixed it. Thank you wOxxOm.