如何使用match返回字符串中的字符?

时间:2021-02-14 20:10:53

I have the following text that is inside of a long string called content.

我有一个名为content的长字符串内的以下文本。

data-seq="0123abcd"

Previously I was using the following to match characters but I am not sure if the correct way to do this and it does not work for number of characters that are not eight.

以前我使用以下内容来匹配字符,但我不确定是否正确的方法来执行此操作,它不适用于非八字符的数字。

var a = content.match(/data-seq="(.{8}).*/)[1]

What I need is for the value of a to set all the characters between the quotes after data-seq and not just eight as in the above.

我需要的是a的值来设置data-seq之后的引号之间的所有字符,而不是如上所述的八个。

2 个解决方案

#1


3  

This is how I would implement it:

这是我实现它的方式:

var a = content.match(/data-seq="([^"]+)"/)[1]

This will capture everything between the double quotes.

这将捕获双引号之间的所有内容。

#2


0  

var a = content.match(/data-seq="([^"][a-zA-Z\-0-9]{3,4,8})"/)[1]

to match only digits or letters (between quotes), precisely 3,4 or 8 chars.

仅匹配数字或字母(引号之间),恰好3,4或8个字符。

#1


3  

This is how I would implement it:

这是我实现它的方式:

var a = content.match(/data-seq="([^"]+)"/)[1]

This will capture everything between the double quotes.

这将捕获双引号之间的所有内容。

#2


0  

var a = content.match(/data-seq="([^"][a-zA-Z\-0-9]{3,4,8})"/)[1]

to match only digits or letters (between quotes), precisely 3,4 or 8 chars.

仅匹配数字或字母(引号之间),恰好3,4或8个字符。