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

javascript - remove everything up to first occurrence of special characters - Stack Overflow

programmeradmin4浏览0评论

helloo... i need to remove everything until first occurrence of special charaters..

say i have string like so..

let str = `28 Mar 2017 ... helloo ... i need to remove everything until first occurrence of special charaters ...`

i need to remove 28 Mar 2017 ... thus being helloo ... i need to remove everything until first occurrence of special charaters ...

i did

str.split(" ... ").pop()

it just remove everything when " ... " occur, instead of first occurrence... it only apply when there have only 1 " ... "

regex is also wele..

thank Youu..

helloo... i need to remove everything until first occurrence of special charaters..

say i have string like so..

let str = `28 Mar 2017 ... helloo ... i need to remove everything until first occurrence of special charaters ...`

i need to remove 28 Mar 2017 ... thus being helloo ... i need to remove everything until first occurrence of special charaters ...

i did

str.split(" ... ").pop()

it just remove everything when " ... " occur, instead of first occurrence... it only apply when there have only 1 " ... "

regex is also wele..

thank Youu..

Share Improve this question asked Mar 28, 2017 at 8:23 karinakarina 7572 gold badges9 silver badges16 bronze badges 1
  • Does this answer your question? Remove part of the string before the FIRST dot with js – Heretic Monkey Commented Aug 4, 2021 at 15:20
Add a ment  | 

4 Answers 4

Reset to default 3

This works by using the ? of regex.

Full regex break down:

  • ^ the start of the string
  • .+ match any character unlimited times
  • ? Matches the preceding expression 0 or 1 times
  • (\.\.\.) ... where the . is escaped by \
  • the last space of your regex

then use String.prototype.replace to replace the set described by regex with an empty string.

See the regex here.

let str = `28 Mar 2017 ... helloo ... i need to remove everything until first occurrence of special charaters ...`

console.log(str.replace(/^.+?(\.\.\.) /, ''))

The naive solution:

    var str = "28 Mar 2017 ... helloo ... i need to remove everything until 
             first occurrence of special charaters ...";
    var res = str.substring(str.indexOf('...')+3, str.length);

Regards.

If you want to use split you should use shift() which will remove the first item in an array. pop() is removing the last one

let str = `28 Mar 2017 ... helloo ... i need to remove everything until first occurrence of special charaters ...`;

let split = str.split(' ... '); //Split by characters needed
split.shift(); //Shift first index => everything before characters

console.log(split.join(" "));

Use the replace function and pass the regex [^.]+[\.]+\s* to replace everything before the first letter after the ... and the optional space

Look, we didn't used the g flag here to match only first occurance.

Then, replace it with ''

var str = '28 Mar 2017 ... helloo ... i need to remove everything until first occurrence of special charaters ...';
console.log(str.replace(/[^.]+[\.]+\s*/,''));

发布评论

评论列表(0)

  1. 暂无评论