简体   繁体   English

jQuery - 获取括号内的值

[英]jQuery - get values inside parentheses

I got a HTML Table with values inside each row.我得到了一个 HTML 表,每行内都有值。
The datas inside the consist the following format:里面的数据包括以下格式:

ABCD (1000.50) ABCD (1000.50)

$('.tdclass').each(function(index){
  var test = push($(this).text().match(?regex?));
});

Well, regex is definitely not one of my strength;-)好吧,正则表达式绝对不是我的强项之一;-)
What is the according regex to get the values inside the parentheses?获取括号内值的相应正则表达式是什么?

Any help would be appreciated!任何帮助,将不胜感激!

If the first part of a string is a fixed length, you can avoid complicated procedures entirely using slice() :如果字符串的第一部分是固定长度,则可以完全使用slice()避免复杂的过程:

var str = "ABCD (1000.50)";

alert(str.slice(6, -1));
//-> "1000.50"

Otherwise, you can use indexOf() and, if you need it, lastIndexOf() to get the value:否则,您可以使用indexOf() ,如果需要,可以使用lastIndexOf()来获取值:

var str = "ABCDEFGHIJKLMNO (1000.50)",
    pos = str.indexOf("(") + 1;

alert(str.slice(pos, -1));
//-> "1000.50"

alert(str.slice(pos, str.lastIndexOf(")");
//-> "1000.50"

A minimal regex of \((.*)\) would do the trick, however this doesn't account for multiple brackets, and there's no need to include 4 characters prior to that. \((.*)\)的最小正则表达式可以解决问题,但这不考虑多个括号,并且在此之前不需要包含 4 个字符。 It literally says 'match (, followed by as many non-new-line characters as possible, followed by )'它的字面意思是“匹配(,后跟尽可能多的非换行符,然后是)”

Look here: Using jQuery to find a substring看这里: 使用 jQuery 找到 substring

But substring(), indexof() might be easier than regex.但是substring(), indexof()可能比正则表达式更容易。

http://www.w3schools.com/jsref/jsref_indexof.asp http://www.w3schools.com/jsref/jsref_indexof.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM