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

191
Views
Javascript extract string from start to 0/, removing anything after 0/

I have a string which looks like this:

file:///storage/emulated/0/Pictures/IMG212.jpg

I want to remove anything from Pictures onwards so I would get this:

file:///storage/emulated/0/

How can I do this?

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

0

var regex = /^.*0\//
var matches = str.match(regex);
console.log(matches[0]);
about 4 years ago · Juan Pablo Isaza Report

0

You could find the index of /0 and use substring to select everything up to that point, like so:

const line = "file:///storage/emulated/0/Pictures/IMG212.jpg";

// Get the location of "/0", will be 24 in this example
const endIndex = line.indexOf("/0");

// Select everything from the start to the endIndex + 2
const result = line.substring(0, endIndex+2);

The reason why we add +2 to the endIndex is because the string that we're trying to find is 2 characters long:

file:///storage/emulated/0/Pictures/IMG212.jpg
                  [24]--^^--[25]
about 4 years ago · Juan Pablo Isaza Report

0

You can split the string using split method:

const myString = "file:///storage/emulated/0/Pictures/IMG212.jpg";
const result = myString.split("0/")[0] + "0/";
console.log(result);    // file:///storage/emulated/0/

documentation for split method on MDN

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!