Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript text between double quotes

I would like to get the text between double quotes using JavaScript. I found online something like title.match(/".*?"/); but the thing is that sometimes I have text between double quotes but sometimes there are no quotes. What I am saying is that sometimes I receive strings like: Neque porro quisquam est qui dolorem ipsum and sometimes strings like: Neque "porro quisquam est" qui dolorem ipsum. The thing is, when I have text containing double quotes I want to retrieve the text between them but when they aren't present, I'd like the whole text. Also I have observered that string.indexOf("\"") does not work and I don't really know how to approach this problem. Thanks.

like image 242
Teo Avatar asked Nov 05 '13 15:11

Teo


People also ask

How do you split a string with double quotes?

split("(? =\"[^\"]. *\")");

How do you use double quotes in JavaScript?

to show double quote you can simple use escape character("\") to show it.

When assigning text to strings you can use single or double quotes?

use double quotes for strings and single quotes for chars. I prefer to use the same quoting across the board and so stick with double quotes for JS.

How do you escape a double quote in JavaScript?

To escape a single or double quote in a string, use a backslash \ character before each single or double quote in the contents of the string, e.g. 'that\'s it' . Copied!


4 Answers

Try:

<script>
let str1 = 'Neque porro quisquam est qui dolorem ipsum';
let str2 = 'Neque "porro quisquam est" qui dolorem ipsum';
let str3 = 'Neque "porro';
let str4 = 'Neque "porro" quisquam "est" qui dolorem ipsum';

function extractFirstText(str){
  const matches = str.match(/"(.*?)"/);
  return console.log(matches
    ? matches[1]
    : str);
}


function extractAllText(str){
  const re = /"(.*?)"/g;
  const result = [];
  let current;
  while (current = re.exec(str)) {
    result.push(current.pop());
  }
  return console.log(result.length > 0
    ? result
    : [str]);
}

// Execution of the functions

extractFirstText(str1);
//Neque porro quisquam est qui dolorem ipsum

extractFirstText(str2);
//porro quisquam est

extractFirstText(str3);
//Neque "porro

extractFirstText(str4);
//porro

extractAllText(str1);
//Array [ "Neque porro quisquam est qui dolorem ipsum" ]

extractAllText(str2);
//Array [ "porro quisquam est" ]

extractAllText(str3);
//Array [ "Neque \"porro" ]

extractAllText(str4);
//Array [ "porro", "est" ]
</script>

EDIT reworked to take into account both @AshishMaity comment in a discarded edit about matching more than one substring, and @JosephCho comment about the original breaking in case there is a single quote (str3 in the case above)

like image 179
gotofritz Avatar answered Sep 20 '22 15:09

gotofritz


try it with this one:

/"((?:\\.|[^"\\])*)"/

Regular expression visualization

Debuggex Demo

like image 28
bukart Avatar answered Sep 17 '22 15:09

bukart


In a single regex:

var m = s.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");

TEST:

s = 'Neque "porro quisquam est" qui dolorem ipsum';
m = s.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");
//=> porro quisquam est

s = 'Neque porro quisquam est qui dolorem ipsum';
m = s.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");
//=> Neque porro quisquam est qui dolorem ipsum
like image 31
anubhava Avatar answered Sep 17 '22 15:09

anubhava


split() function can be used get value in double quote

let str1 = 'Neque "porro quisquam est" qui dolorem ipsum';
let str2 = 'Neque porro quisquam est qui dolorem ipsum';

function findFirstOccurance(str){
  const matches = str.split('"');
  return matches[1] ? matches[1] : str;
}


console.log(findFirstOccurance(str1));
console.log(findFirstOccurance(str2)); 
like image 37
Manasi Avatar answered Sep 19 '22 15:09

Manasi