coderkid coderkid - 6 months ago 11
Javascript Question

How to remove everything up until the first character in Javascript?

I'm getting a string which returns an integer first, followed by a series of spaces and then the string I want. My goal is to just get "Moonwalking with Einstein" in this example. I'm using Javascript. What's the best way to achieve this?

2







Moonwalking with Einstein

Answer

You can use JavaScript's replace function for this.

It looks like you are looking to replace digits and instances of two or more consecutive spaces

\d - Matches any digit

| - Or conditional symbol

\s - Matches any whitespace character

{2,} - Matches two or more

/g - Matches all instances

var string = " 2                    Moonwalking with Einstein"

string = string.replace(/\d|\s{2,}/g, '')

var result = document.getElementById("result");

result.innerHTML = string;
<p id="result"></p>