settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Extract Text in JavaScript

+1 vote

You will be given a text as a string. Write a JavaScript function that extracts and prints only the text that’s surrounded by parentheses. The input comes as a single string argument.

Examples:

Input:
'Rakiya (Bulgarian brandy) is self-made liquor (alcoholic drink)'

Output:
Bulgarian brandy, alcoholic drink

The output is printed on the console on a single line, in the form of a comma-separated list.

asked in JavaScript category by user eiorgert

1 Answer

0 votes

Here i s my js code:

function brackets(str) {
    let results = [];
    let rightBracket = -1;
    while (true) {
        let leftBracket = str.indexOf("(", rightBracket + 1);
        if (leftBracket == -1) {
            break
        }
        rightBracket = str.indexOf(")", leftBracket - 1);
        if (rightBracket == -1) {
            break
        }
        else {
            let textInside = str.substring(leftBracket + 1, rightBracket);
            results.push(textInside);
        }
    }
    console.log(results.join(", "));
}

brackets("Rakiya (Bulgarian brandy) is self-made liquor (alcoholic drink)");

 

answered by user hues
...