How to get substring between two same characters in JavaScript? - Stack Overflow

I have a string value as abc:language-letters-alphsEnglishData:7844val: . I want to extract the part l

I have a string value as abc:language-letters-alphs/EnglishData:7844val: . I want to extract the part language-letters-alphs/EnglishData, the value between first : and second :. Is there a way to do it without storing each substrings on different vars? I want to do it the ES6 way.

I have a string value as abc:language-letters-alphs/EnglishData:7844val: . I want to extract the part language-letters-alphs/EnglishData, the value between first : and second :. Is there a way to do it without storing each substrings on different vars? I want to do it the ES6 way.

Share Improve this question edited Aug 13, 2020 at 7:43 Always Helping 14.6k4 gold badges15 silver badges30 bronze badges asked Aug 13, 2020 at 5:23 DragonDragon 1,30418 silver badges26 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 5

You can do this two ways easily. You can choose what suits you best.

Using String#split

Use split method to get your desired text.

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.

let str = 'abc:language-letters-alphs/EnglishData:7844val:'.split(':')

console.log(str[1]) //language-letters-alphs/EnglishData

Using String#slice

You can use [ Method but in that you have define the exact indexes of the words you want to extract.

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

let str = 'abc:language-letters-alphs/EnglishData:7844val:'

console.log(str.slice(4, 38)) //language-letters-alphs/EnglishData

const str = "abc:language-letters-alphs/EnglishData:7844val:"
const relevantPart = str.split(':')[1]

console.log("abc:language-letters-alphs/EnglishData:7844val:".split(":")[1])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744891503a4599441.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信