coderkid coderkid - 1 year ago 49
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?


Moonwalking with Einstein

Answer Source

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>