0

Let's say I had some HTML that looks like the following:

<input type="text" data-text="mytext{someothertext}">

How would I use a Regular Expression to 'pull out' what's inside the curly braces inside the 'data-text' attribute. I know this is probably looking rather bizarre and also weird, but I am trying something out in a new project. And the curly braces here are not to represent JSON or anything like it.

What I want to be able to do is console.log() what's inside of the curlies!

Thank you! :)

4

1 回答 1

0

你可以这样做:

var myText= myElement.dataset['text'].match(/{([^}]+)}/)[1]

这设置myText"someothertext"

如果您不能使用数据集(例如,因为您想与旧浏览器兼容),那么您必须使用 getAttribute :

var myText= myElement.getAttribute('data-text').match(/{([^}]+)}/)[1]
于 2013-07-28T18:03:02.053 回答