I would like to manipulate the captured data from the regex return
I am writing a command that will search my html document and add an id to any title tag based on the text node. I wanted to know if it is possible to capture the text of a node and then replace the space with hyphens.
eg.
<h2>This is a heading</h2>
will become
<h2 id="this-is-a-heading">This is a heading</h2>
I am new to regex, so forgive me if this is a stupid question.
At the moment I have this, but understand that it does not modify the captured data.
Search criteria:
<h2\s*>([^<]*)</h2>
Replace text:
<h2 id="$1">$1</h2>
Thanks in advance.
+3
source to share
2 answers
You probably don't really need a regex for this, but you can do it.
Normal JavaScript example
window.onload = function() {
var element = document.getElementById("somenode");
var text = element.innerHTML;
element.innerHTML = text.replace(" ", "-");
};
<h1 id="somenode">Hello world!</h1>
0
source to share
I had to use the job while I was doing what I wanted in stages.
This is what I have done so far.
in order to change
<h2>1. Something Here</h2>
to
<h2 id="Something-here">1. Something Here</h2>
To find all heading tags excluding h1 ...
dw.setUpFindReplace({searchString: '<h([^1])>([^<]*)',
replaceString: '<h$1 id="$2">$2', searchSource: true,
matchCase: false, ignoreWhitespace: false,
useRegularExpressions: true, searchWhat: 'document'});
dw.replaceAll();
remove any numbers at the beginning of the id ...
dw.setUpFindReplace({searchString: '<h([^1]) id="\s*(\d+\s+|\d+,\d+\s+|\d+\.)',
replaceString: '<h$1 id="', searchSource: true, matchCase: false,
ignoreWhitespace: false, useRegularExpressions: true,
searchWhat: 'document'});
dw.replaceAll();
to carry the identifier ...
dw.setUpFindReplace({searchString: '<h([^1]) id="(\w+)\s+(\w+)\s*',
replaceString: '<h$1 id="$2-$3',
searchSource: true, matchCase: false,
ignoreWhitespace: false, useRegularExpressions: true,
searchWhat: 'document'});
dw.replaceAll();
0
source to share