English 中文(简体)
Java 经常出口,在[]之间返回
原标题:JavaScript RegularExpression to return string between [ ]

我目前正在利用 j和 Java文开展一个项目。 将在OMS内有多个<代码>a要素,这些要素可使用rel属性,例如jqgal[mygal1]

Once one of the anchors is clicked, I want to extract the value between the squared brackets, and read it into a variable for use later in the script. So for example in the following markup:

<a href=“my_image_1.jpg” rel=“jqgal[mygal1]”><img src=“some_image.jpg”/></a>

常规压缩应退还<条码>mygal1。 我一直在试图说明这一点,迄今为止,已经取得了很多进展(这是其中之一)。 是否有任何人表示采取这一行动的正确定期表述? 如果它有所帮助,则在方括号外加插始终为<编码>jqgal[。

最佳回答

这样做有助于:

$( a[rel^="jqgal"] ).click(function () {
    var val = $(this).attr("rel").match(/jqgal[(.*)]/)[1];
    // Do what you need with the  val 
});
问题回答

Use /^.*?[(.*?)]/ regex on your rel attribute. The content of the group 1 (captured by the first parenthesis group) should be what you want.

var data = /^.*?[(.*?)]$/.exec(link.rel)[0];
var str = "jqgal[mygal1]"
var re = /[([^]]+)]/
console.log( str.match(re) );

解释性格

[       find a [ character - the  character escapes the [ so it matches the text
(        capture group start
[^]]+   match any character that is not a ]
)        capture group end
]       match a closing [ (not needed) 

使用这一条码:<代码>/jqgal[(*)]/

它将在<条码>jqgal[]内搜寻任何座标。

例:

"jqgal[mygal1]".match(/jqgal[(.*)]/)
> ["jqgal[mygal1]", "mygal1"]




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签