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

javascript - split on non-consistent string - Stack Overflow

programmeradmin2浏览0评论

I have these value

['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4']

How do I get their numberic number which appended after the last _? I can't do str.split('_')[1], because some of the value does have 2 _, either can I use slice to get last character, what if the numberic value is more than 1 character.

I have these value

['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4']

How do I get their numberic number which appended after the last _? I can't do str.split('_')[1], because some of the value does have 2 _, either can I use slice to get last character, what if the numberic value is more than 1 character.

Share Improve this question edited Oct 1, 2019 at 12:51 Eddie 26.9k6 gold badges38 silver badges59 bronze badges asked Oct 1, 2019 at 12:48 Charles Jr.Charles Jr. 1271 silver badge5 bronze badges 2
  • 3 Try .split('_').pop() – User863 Commented Oct 1, 2019 at 12:50
  • var numbers = values.map(value.split("_").pop()) – JsCoder Commented Oct 1, 2019 at 12:52
Add a ment  | 

9 Answers 9

Reset to default 7

let values = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4'];
let numbers = values.map(value => value.split("_").pop());
console.log(numbers);

You can use a regex for this: /_(\d+)$/

const regex = /_(\d+)$/;
const books = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4']

const numbers = books.map(book => book.match(regex)[1]);
console.log(numbers);

The regex works like this:

 _                 match a single _

  (                  start a matching group
    \d+                match one or more digits
  )                  close matching group
$                  assert end of string

You don't necessarily need the beginning _, it's up to you if that fits your usecase

Using lastIndexOf()

let values = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4']

let result = values.map(str => str.substring(str.lastIndexOf('_') + 1))

console.log(result);

You could match the last digits.

var array = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4'],
    numbers = array.map(s => +s.match(/\d+$/));

console.log(numbers);

So either you use pop method on split method like

var digit = str.split('_').pop()

OR get the last element from the array returned by split

var spStr = str.split('_');
var digit = spStr[spStr.length-1]

You can use lastIndexOf and slice functions

const arr = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4'];

const strs = arr.map(str => {
  let i = str.lastIndexOf("_");
  return str.slice(i+1);
});

console.log(strs);

I believe @Philips regex solution and @Aurel Bílý solutions are great. Here is a solution that is kind of fun

const books = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4'];
const bookNumbers = books.map((book) => book.split('_').reverse()[0]);

Admittedly not the most performant, but yet another way to do it!

try this below Code Snip

var values = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4'];
for (i = 0; i < values.length; i++) {
  console.log(values[i].substring(values[i].lastIndexOf('_') + 1));
}

You could join first, and then .match(/\d+(?=,|$)/g) to get all the digits in the string delimeted by ,:

const books = ['book_1', 'reader_2', 'book_borrower_3', 'book_reader_borrower_4'];
const res = books.join().match(/\d+(?=,|$)/g);

console.log(res);

发布评论

评论列表(0)

  1. 暂无评论