return the first non repeating character in a string in javascript

后端 未结 26 2450
清酒与你
清酒与你 2020-12-30 09:04

So I tried looking for this in the search but the closest I could come is a similar answer in several different languages, I would like to use Javascript to do it.

T

26条回答
  •  抹茶落季
    2020-12-30 09:11

    You can iterate through each character to find() the first letter that returns a single match(). This will result in the first non-repeated character in the given string:

    const first_nonrepeated_character = string => [...string].find(e => string.match(new RegExp(e, 'g')).length === 1);
    const string = 'aabcbd';
    
    console.log(first_nonrepeated_character(string)); // c

提交回复
热议问题