0

Say I have a string like so:

let v = '/////bar/foo'

how can I just replace the slashes at the beginning of v with ''?

I am looking to get this:

let result = 'bar/foo'
1
  • How do you define "any beginning character instance"? It seems you want to replace all non–alphabetic characters, which might be /^[^a-z]+/i. Commented Jun 30, 2017 at 1:57

3 Answers 3

4
let r = v.replace(/^\/+/, '');

The regular expression finds one or more / at the beginning of the string and replaces that pattern with ''.

Sign up to request clarification or add additional context in comments.

Comments

1

A little regex and you're done:

let v = '/////bar/foo';

console.log( v.replace(/^\/+/,'') );

  • in \/, the \ escapes the forward slash
  • the + means 1 or more
  • the '' is the replacement string

Comments

0
let parsed = v.replace(/^[^a-zA-Z\d]+/, '')

This will replace any character that is not alphanumeric

or as pointed out by RobG

/^\W+/

2 Comments

/^\W+/ is shorter. ;-)
@RobG Oh fine :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.