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

Q: Extract Parenthesis Task in HTML with DOM and JavaScript

+2 votes

Write a JS function that when executed, extracts all parenthesized text from a target paragraph by given element ID. The result is a string, joined by "; " (semicolon, space).

Your function will receive a string parameter, representing the target element ID, from which text must be extracted. The text should be extracted from the DOM.

Return a string with all matched text, separated by "; " (semicolon, space).


extract parenthesis javascript task

asked in JavaScript category by user icabe

1 Answer

+1 vote

Here is my solution:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
<p id="content">
    Rakiya (Bulgarian brandy) is home-made liquor (alcoholic drink). It can be made of grapes, plums or other fruits
    (even apples).

<p id="holder">
    Lorem ipsum dolor sit amet, (consectetur adipiscing elit), sed do eiusmod (tempor) incididunt ut labore (et dolore
    magna) aliqua.

 function extract(elementId){
     let para = document.getElementById(elementId).textContent;
     let pattern=/\(([^)]+)\)/g;
     let result = [];

     let match = pattern.exec(para);
     return result.join("; ")



answered by user john7