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

343
Views
How to separate symbols from text with regex?

I have a textNode in which i have the data coming like this data=$(2000)%,so i want to separate out the symbols and number.

let preText = ''
let number = ''
let postText = ''
let data = '$(2000)%'

const regex = new RegExp(`(\(\[)?[0-9]{1,3}(?:,[0-9]{1,3})*(\.[0-9]{1,5})?(\)\])?`); 

data.match(regex)

Output expected:
preText = '$('
number = '2000'
postText = ')%'

Another example: if data=$2,23,603, the output should be preText=$, number=2,23,603 and postText=.

I'm not able to achieve this with different variables,how can i get the desired output?

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

0

You can use

const data = '$(2000)%';
const regex = /^(.*?)((?:\d{1,3}(?:,\d{1,3})*|\d+)(?:\.\d{1,5})?)(\D.*)?$/; 
const [_, preText, number, postTex] = data.match(regex);
console.log([preText, number, postTex]);

Details:

  • ^ - start of string
  • (.*?) - Group 1 (preText): any zero or more chars other than line break chars, as few as possible
  • ((?:\d{1,3}(?:,\d{1,3})*|\d+)(?:\.\d{1,5})?) - Group 2 (number): one to three digits followed with zero or more occurrences of a comma and one to three digits, or just one or more digits, and then an optional sequence of a . and one to five digits
  • (\D.*)? - Group 3 (postTex), optional: a non-digit char and then any zero or more chars other than line break chars, as many as possible
  • $ - end of string.
about 4 years ago · Juan Pablo Isaza Report

0

Some notes about the pattern:

  • First testing you code, this is the error message:

Uncaught SyntaxError: Invalid regular expression: /(([)?[0-9]{1,3}(?:,[0-9]{1,3})*(.[0-9]{1,5})?()])?/: Unterminated group

  • Apart from that, you have to double escape the backslash in the RegExp constructor
  • Between the parenthesis, there is 2000 which is 4 digits, and [0-9]{1,3} can only match 1-3 digits.
  • A pattern like \(\[ will only match when both ([ are present
  • The pattern does not match $ or % so that can not be in the output

What you could do it either capture all the possible symbols in groups on the left and the right using a character class like [$\[(]

([$\[(]*)\b((?:[0-9]{1,3}(?:,[0-9]{1,3})*(?:\.[0-9]{1,5})?)|\d+)\b([\])%]*)

Regex demo

Or optionally match all non digits \D* around matching the digits:

(\D*)\b((?:[0-9]{1,3}(?:,[0-9]{1,3})*(?:\.[0-9]{1,5})?)|\d+)\b(\D*)

Regex demo

If there is a match, the capture group numbers are 1, 2 and 3:

let data = '$(2000)%'
const regex = /([$\[(]*)\b((?:[0-9]{1,3}(?:,[0-9]{1,3})*(?:\.[0-9]{1,5})?)|\d+)\b([\])%]*)/;
const m = data.match(regex);
console.log(m);

if (m) {
  preText = m[1];
  // etc..
}

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!