I need help with regex. I need to create a rule that would preserve everything between quotes and exclude quotes. For example: I want this...
STRING_ID#0="Stringtext";
...turned into just...
Stringtext
Thanks!
I need help with regex. I need to create a rule that would preserve everything between quotes and exclude quotes. For example: I want this...
STRING_ID#0="Stringtext";
...turned into just...
Stringtext
Thanks!
The way to do this is with capturing groups. However, different languages handle capturing groups a little differently. Here's an example in Javascript:
var str = 'STRING_ID#0="Stringtext"';
var myRegexp = /"([^"]*)"/g;
var arr = [];
//Iterate through results of regex search
do {
var match = myRegexp.exec(str);
if (match != null)
{
//Each call to exec returns the next match as an array where index 1
//is the captured group if it exists and index 0 is the text matched
arr.push(match[1] ? match[1] : match[0]);
}
} while (match != null);
document.write(arr.toString());
The output is
Stringtext
Try this
function extractAllText(str) {
const re = /"(.*?)"/g;
const result = [];
let current;
while ((current = re.exec(str))) {
result.push(current.pop());
}
return result.length > 0 ? result : [str];
}
const str =`STRING_ID#0="Stringtext"`
console.log('extractAllText',extractAllText(str));
document.body.innerHTML = 'extractAllText = '+extractAllText(str);
"([^"\\]*(?:\\.[^"\\]*)*)"
I recommend reading about REGEXes here