J'ai une exigence. J'ai une chaîne qui a une valeur pour par exemple:Manipulation de chaînes - éditeur de texte enrichi
<p>We are pleased <a href="http://www.anc.com/content/cy-tech/global/en/cq5-reference-materials.s_cid_123.html">to present the new product type</a>. This new product type is the best thing since sliced bread. We are pleased to present the new product type. This new product <a href="mailto:[email protected]">type is the best</a> thing since sliced bread.</p>
Le texte ci-dessus sera stocké sous forme d'une seule valeur de chaîne. J'ai besoin d'ajouter certains paramètres aux hrefs après avoir vérifié les critères. Faites-moi savoir comment extraire uniquement le href et ajouter le paramètre et afficher la chaîne sans dommage (FYI: la chaîne est la valeur entrée via RTE - éditeur de texte riche)
Essayé cette approche mais sans succès.
String tmpStr = "href=\"http://www.abc.com\">design";
StringBuffer tmpStrBuff = new StringBuffer();
String[] tmpStrSpt = tmpStr.split(">");
if (tmpStrSpt[0].contains("abc.com")) {
String[] tmpStrSpt1 = tmpStrSpt[0].split("\"");
tmpStrBuff.append(tmpStrSpt1[0]);
if (tmpStrSpt1[1].contains("?")) {
tmpStrBuff.append("\"" + tmpStrSpt1[1] + "&s_cid=abcd_xyz\">");
} else {
tmpStrBuff.append("\"" + tmpStrSpt1[1] + "?s_cid=abcd_xyz\">");
}
tmpStrBuff.append(tmpStrSpt[1]);
tmpStrBuff.append("</a>");
System.out.println(" <p>tmpStr1:::: " + tmpStrBuff.toString() + "</p>");
}
l'autre approche utilisée est:
String[] tmpTxtArr = text.split("\\s+");
StringBuffer tmpStrBuff = new StringBuffer();
for (String tmpTxt : tmpTxtArr) {
descTxt += (tmpTxt.contains("abc.com") && !tmpTxt.contains("?")) ? tmpTxt
.replace("\">", "?s_cid=" + trackingCode + "\">" + " ")
: tmpTxt + " ";
}
Comment est-ce lié à Java? – Lion
J'ai besoin du code en Java. – user1661908
Eh bien au moins, vous devriez rejeter regex. L'analyse du langage de balisage avec regex n'est pas une très bonne idée. Aussi pourquoi Javascript? – Mena