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

javascript - Remove part of the string before the FIRST dot with js - Stack Overflow

programmeradmin1浏览0评论

I have the next problem. I need to remove a part of the string before the first dot in it. I've tried to use split function:

var str = "P001.M003.PO888393"; 
str = str.split(".").pop();

But the result of str is "PO888393".

I need to remove only the part before the first dot. I want next result: "M003.PO888393".

Someone knows how can I do this? Thanks!

I have the next problem. I need to remove a part of the string before the first dot in it. I've tried to use split function:

var str = "P001.M003.PO888393"; 
str = str.split(".").pop();

But the result of str is "PO888393".

I need to remove only the part before the first dot. I want next result: "M003.PO888393".

Someone knows how can I do this? Thanks!

Share edited Jan 26, 2021 at 12:07 Nick Parsons 51k6 gold badges57 silver badges75 bronze badges asked Jan 26, 2021 at 12:02 jciaurriz_iarjciaurriz_iar 231 silver badge3 bronze badges 1
  • If you know the characters before the first dot are always going to be four, you can try with substring(), like so str.substring(4) – Vincenzo Commented Jan 26, 2021 at 12:10
Add a ment  | 

6 Answers 6

Reset to default 6

One solution that I can e up with is finding the index of the first period and then extracting the rest of the string from that index+1 using the substring method.

let str = "P001.M003.PO888393"; 
str = str.substring(str.indexOf('.')+1);
console.log(str)

You could use a regular expression with .replace() to match everything from the start of your string up until the first dot ., and replace that with an empty string.

var str = "P001.M003.PO888393"; 
var res = str.replace(/^[^\.]*\./, '');
console.log(res);

Regex explanation:

  • ^ Match the beginning of the string
  • [^\.]* match zero or more (*) characters that are not a . character.
  • \. match a . character

Using these bined matches the first characters in the string include the first ., and replaces it with an empty string ''.

You can use split and splice function to remove the first entry and use join function to merge the other two strings again as follows:

str = str.split('.').splice(1).join('.');

Result is

M003.PO888393

var str = "P001.M003.PO888393";
str = str.split('.').splice(1).join('.');
console.log(str);

calling replace on the string with regex /^\w+\./g will do it:

let re = /^\w+\./g
let result =  "P001.M003.PO888393".replace(re,'')
console.log(result)

where:

  • \w is word character
  • + means one or more times
  • \. literally .

many way to achieve that:

  • by using slice function:
    let str = "P001.M003.PO888393";
    str = str.slice(str.indexOf('.') + 1);
  • by using substring function
    let str = "P001.M003.PO888393"; 
    str = str.substring(str.indexOf('.') + 1);
  • by using substr function
    let str = "P001.M003.PO888393";
    str = str.substr(str.indexOf('.') + 1);
  • and ...

Using String.prototype.replace() on a RegExp with the non-greedy quantifier *?:

> 'P001.M003.PO888393'.replace(/.*?\./, '')
'M003.PO888393'
发布评论

评论列表(0)

  1. 暂无评论