rumDMC
rumDMC

Reputation: 23

Trying to splice several words inside of a string

I'm an absolute beginner with javascript and need someone to hold my hand. I have a string that I am trying to splice several words out of, and need help doing so. It's probably easier if I use an example:

What I have:

"Testing::testing2::testing3 Sample::sample2::sample3"

What I want spliced out:

"testing3 sample3"

The code that I am using right now will only display the last word (sample3), but I need it to show the last word before the 'space' in both sequences. Help?

Current code:

var the_string = "Testing::testing2::testing3 Sample::sample2::sample3";
var parts = the_string.split('::').slice(-1);
document.getElementById('tagslast').innerHTML = parts;

Upvotes: 2

Views: 64

Answers (1)

Spectric
Spectric

Reputation: 32000

You can split the string by a space, then split each individual item by :: and get the last items:

const str = "Testing::testing2::testing3 Sample::sample2::sample3";

const result = str.split(" ").map(e => e.split("::").slice(-1)[0])

console.log(result)

Upvotes: 4

Related Questions