You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dynatrace中JS正则问题:请求修正含额外字符的元素点击jQuery代码

Fixing jQuery Click for Elements with Hidden Soft Hyphens in Text

Hey Maher, sorry to hear this selector broke after the site upgrade! Those hidden soft hyphens (the ­ entities that show up as odd breaks in your text) are the culprit here—jQuery's :contains() selector does an exact substring match, so those extra hidden characters throw it off completely.

Here are a few reliable fixes to handle this:

1. Clean Text Before Matching with filter()

Use jQuery's filter() method to strip out soft hyphens (and any other unwanted entities) from the element's text before comparing it to your target string:

$("div").filter(function() {
  // Remove soft hyphens (\u00AD is the Unicode for ­) and stray & entities
  const cleanedInnerText = $(this).text().replace(/[\u00AD&]/g, "").trim();
  // Compare to your target text without any extra characters
  return cleanedInnerText === "Create Purchase Requisition";
}).click();

This works by normalizing the element's text to match your expected string, ignoring those hidden hyphens and entities.

2. Fuzzy Match with Regular Expressions

If you want to be even more flexible (in case other hidden characters get added later), use a regex to match the words in order, allowing any number of hidden characters between them:

$("div").filter(function() {
  const elementText = $(this).text();
  // Match each word segment, allowing soft hyphens or whitespace in between
  const matchPattern = /Create[\s\u00AD]*Pur[\s\u00AD]*chase[\s\u00AD]*Req[\s\u00AD]*ui[\s\u00AD]*si[\s\u00AD]*tion/;
  return matchPattern.test(elementText);
}).click();

The [\s\u00AD]* part matches any combination of whitespace or soft hyphens between the word fragments, so even if the text is split with more hidden characters, it will still find the right element.

3. Use More Stable Element Attributes (If Possible)

As a best practice, if the element has a unique class, ID, or data attribute, use that instead of text-based selectors—these are far less likely to break when the site gets updated:

// Example using a class (adjust to match your element's actual class)
$("div.create-purchase-req-btn").click();

// Example using a data attribute
$("div[data-action='create-purchase-requisition']").click();

This avoids text-related issues entirely and makes your code more robust.

Any of these should get your click action working again. Let me know if you need help tweaking the regex or finding the right attributes for your element!

内容的提问来源于stack exchange,提问作者Maher Hussain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 13:57:27