Home > Mobile >  How to remove a substring enclosed inside double quotes [duplicate]
How to remove a substring enclosed inside double quotes [duplicate]

Time:09-18

I have below string

"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html></html>"

in which I want to remove the below part using JavaScript

PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"

such that the result should be

<!DOCTYPE HTML><html></html>

I have tries something like

console.log('Data', data);
data.replace("PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN" /"http://www.w3.org/TR/html4/loose.dtd/", "");
console.log('Data', data);

where data contains the complete string

but unfortunately both the logs print the same value.

thanks in advance

CodePudding user response:

Variant 1:

const str = `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html></html>`;
const index1 = str.indexOf('HTML');
const index2 = str.indexOf('<html>');
const result = `${str.slice(0, index1   4)}${str.slice(index2 - 1)}`;
console.log(result);

Variant 2:

const str = `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html></html>`;
const index = str.indexOf('><html>');
const result = `${str.slice(0, 14)}${str.slice(index)}`;
console.log(result);

Variant 3 (without $):

const str = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html></html>';
const index = str.indexOf('><html>');
const firstPart = str.slice(0, 14);
const secondPart = str.slice(index);
const result = firstPart   secondPart;
console.log(result);

CodePudding user response:

Adding the HTML as string and replacing the part that you want to remove.

let str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html></html>";
str = str.replace(" PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"", "");

console.log(str);

CodePudding user response:

Here's a simple code to ignore quotation marks and the text between them. If starting quote is identified, flag is set true. While flag == true we ignore the characters. Finally when ending quote is identified, flag is set false.

const str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html></html>"
var flag = false
var newStr = ""
for (let i = 0; i < str.length; i  ){
  if(str[i] == '"') flag = !flag
  if(!flag && str[i]!='"') newStr  = str[i]
}
console.log(newStr)

Hopefully it fulfils your requirement. If you want to handle nested quotes, then let me know that.

CodePudding user response:

If you do not have initial or final quotes:

let str = `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html></html>`;
str = str.replace(`PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"`, '');
console.log(str);

But I suspect you are looking for something more, so please elaborate.

  • Related