Search code examples
javascriptstringfunctionsubstringsubstr

Removing Text from Part of String


I am needing to remove text within different strings.

I need a function that will make the following...

test: example1
preview: sample2
sneakpeak: model3
view: case4

...look like this:

example1
sample2
model3
case4

I have tried using the substr and substring functions but was not able to find a solution.

I used selected.substr(0, selected.indexOf(':')) but all that returned to me was the text before the colon. selected is the variable that contains the string of text.

With the strings being different lengths, it's something that cannot be hardcoded either. Any suggestions?


Solution

  • Use split function. split will return an array. To remove spaces use trim()

    var res = "test: example1".split(':')[1].trim();
    
    console.log(res);