I'm having issues trying to isolate and remove the first 3 words from a string.
The string is: "LES B1 U01 1.1 Discussing what kind of job you want"
I need to make it: "1.1 Discussing what kind of job you want"
I was able to get the first word using /^([\w-]+)/
Any help would be appreciated!
PS. I'm using jQuery
I'm having issues trying to isolate and remove the first 3 words from a string.
The string is: "LES B1 U01 1.1 Discussing what kind of job you want"
I need to make it: "1.1 Discussing what kind of job you want"
I was able to get the first word using /^([\w-]+)/
Any help would be appreciated!
PS. I'm using jQuery
Share Improve this question asked Sep 17, 2014 at 18:42 DondadaDondada 4419 silver badges26 bronze badges4 Answers
Reset to default 6To remove first three words, separated by space you can use String's split
and Array's slice
and join
.
"LES B1 U01 1.1 Discussing what kind ...".split(' ').slice(3).join(' ');
You are on the correct track. I have created a regexp type fiddle here to show you have it works.
/^([\S]+)\s([\S]+)\s([\S]+)/g
Essentially what this does is look for any non-space character 1 or more times followed by a whitespace character then non-space 1 or more times, whitespace, then the last set of non-space characters giving you your three words.
var sentence = "LES B1 U01 1.1 Discussing what kind of job you want";
var words = sentence.split(/\s/);
words.shift(); // shift removes the first element in the array
words.shift(); // ..
words.shift(); // ..
alert(words.join(" "));
http://jsfiddle/y7fLytvg/
This is a way to do it.
var str = "LES B1 U01 1.1 Discussing what kind of job you want";
console.log( str.replace(/^(?:\w+\s+){3}/,'') );
Will output 1.1 Discussing what kind of job you want