javascript - 需要去掉不需要的文字

标签 javascript jquery regex

我需要删除此变量中“(”之前和包括“(”的所有文本以及“)”之后和包括“)”的所有文本。

var this_href = $(this).attr('href');

上面的代码产生了这个...

javascript:change_option('SELECT___100E___7',21);

但是它可以在括号内生成任何长度的任何文本。

Regex 解决方案就可以了。

所以在这种情况下,我想以此结束。

'SELECT___100E___7',21

最佳答案

而不是剥离你不想要的东西,你可以匹配你想要保留的东西:

/\((.*?)\)/

解释:

\(    Match an opening parenthesis.
(     Start capturing group.
.*?   Match anything (non-greedy so that it finds the shortest match).
)     End capturing group.
\)    Match a closing parenthesis.

Use like this:

var result = s.match(/\((.*?)\)/)[1];

关于javascript - 需要去掉不需要的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4057639/

相关文章:

javascript - node.js 缓冲区不为空

java - 每次加载 facelets 组件时调用 javascript 函数

javascript - 使用函数参数访问 JavaScript 中的对象

javascript - x-editable按钮,如何编辑特定评论?

jquery - 取消选择数据表上的多行

c++ - MSVC 正则表达式匹配

javascript - myVar 绑定(bind)到哪个元素? `ng-init` 的问题

javascript - PapaParse 在读取 CSV 时返回 undefined

带有标志的 Python re.sub 不会替换所有出现

python - 使用python删除字符串中的特殊字符