最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript regex for first 3 words - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 6

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

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

发布评论

评论列表(0)

  1. 暂无评论