Dondada
Dondada

Reputation: 441

Javascript regex for first 3 words

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

Upvotes: 0

Views: 2426

Answers (4)

Mic1780
Mic1780

Reputation: 1794

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.

Upvotes: 3

Marc J. Schmidt
Marc J. Schmidt

Reputation: 8459

To 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(' ');

Upvotes: 6

j08691
j08691

Reputation: 207881

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

Upvotes: 0

Ryan
Ryan

Reputation: 14649

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.net/y7fLytvg/

This is a way to do it.

Upvotes: 1

Related Questions