Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
How to find last text before character x while character y is still behind y Javascript

I was trying to find an elegant solution to the following problem. I have the following string :

This ) is ) some $ text. ) lkzejflez $ aaeea ) aeee

Existing out of a part that is human readable text before the bold marked bracket and a part that just plain rubbish after the bracket. I need to find the human readable text in string alike this one. I need to get the text before the last bracket (bold marked) that still has a dollar sign (bold marked) behind it. Here is my solution to the problem :

const info = "This)is)$a sentence.)lkzejflez$aaeea)aeee";
let result;

for(let i = 0; i < info.length;i++){
  const char = info[i];
  const after = info.substring(i+1,info.length);
  const otherChance = after.indexOf(')') < after.lastIndexOf('$');
  if(otherChance){continue;};
  const isEndBracket = char ===')';
  const before = info.substring(0,i)
  if(isEndBracket){result = before;break;};
}
console.log(result)

The expected result is 'This)is)$a sentence.' My code does return this result but it uses substr and a forloop where regex could be used. I do however not now how. Does anyone know a more elegant solution to my problem. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try the following regex: ^.*\)(?=.*\$)

Explanation: Match as many characters as possible from the start of the line to the last ) that is followed by a $.

Detailed explanation: Greedily match zero or more of any character except a newline from the start of the line to a ) character that is followed by zero or more of any character except a newline and then a $ sign.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!