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

195
Views
How do I split a value using the split function from javascript to split checking only the first and last occurence

So I need to split an regex that's coming from my database. The value is like the following

regexValue = '/^([0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}|[0-9]{2}\.?[0-9]{3}\.?[0-9]{3}\/?[0-9]{4}\-?[0-9]{2})$/ig'

if you analyze throughly this regex you can see that it have an forward slash '/' between the regex. So if I wanted to split it, using

values.split('/'); 

It would generate more strings than I would like to. For example a simple regex

//would generate three values("", \w+ , ig)    
regexValue = '/\w+/ig'
regexValue.split('/');

Is there a way so I could only split getting the first and last values?

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

If you want to split that string into the regular expression bodyand its flags, you can do that easily with...a regular expression. ๐Ÿ˜ƒ

const parts = /^\s*\/(.*)\/(?:([a-z]*))\s*$/.exec(regexString);
//           A ^
//           B  ^^^
//           C     ^^
//           D       ^^^^
//           E           ^^
//           F             ^^^^^^^^^^^^
//           G                         ^^^
//           H                            ^
  • A: Start of input
  • B: Optional whitespace
  • C: Leading slash of regex "literal"
  • D: Body of the expression
  • E: Ending slash of regex "literal"
  • F: Optional flags
  • G: Optional whitespace
  • H: End of input

Live Example:

const regexString = '/^([0-9]{3}\\.?[0-9]{3}\\.?[0-9]{3}\\-?[0-9]{2}|[0-9]{2}\\.?[0-9]{3}\\.?[0-9]{3}\\/?[0-9]{4}\\-?[0-9]{2})$/ig';

const parts = /^\s*\/(.*)\/(?:([a-z]*))\s*$/.exec(regexString);
if (!parts) {
    console.log("No match");
} else {
    const [, body, flags] = parts;
    console.log(`body:  ${body}`);
    console.log(`flags: ${flags}`);
}


Note that I escaped the backslashes in the string literal, since you said you're reading this from your database, you don't have it in a string literal in your code. It's just that inside a string literal, a backslash is an escape character, so \. and . mean exactly the same thing. To write a string literal defining a string with backslashes in it, you have to escape them as I've done in the snippet above. Again, you've said it comes from your database, so you're fine, but I figured I should escape them in the example to avoid being misleading.

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!