- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindFirstStringInBracket.js
More file actions
Latest commit
35 lines (27 loc) · 860 Bytes
/
Copy pathfindFirstStringInBracket.js
File metadata and controls
35 lines (27 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// findFirstStringInBracket
constfindFirstStringInBracket=(str)=>{
if(str.length===0){
return"";
}
letindexFirstBracketFound=str.indexOf("(");
if(indexFirstBracketFound<0){
return"";
}
letwordsAfterFirstBracket=str.substr(indexFirstBracketFound);
if(!wordsAfterFirstBracket){
return"";
}
letindexClosingBracketFound=wordsAfterFirstBracket.indexOf(")");
if(indexClosingBracketFound<0){
return''
}
returnwordsAfterFirstBracket.substring(1,indexClosingBracketFound);
}
// Alternative using regex
constfindFirstStringInBracketRegex=(str)=>{
if(str.length===0){
return"";
}
conststringWithParentheses=str.match(/\([^)]*\)/).pop();
returnstringWithParentheses.substring(1,--stringWithParentheses.lengh)
}