eruna
April 25, 2014, 9:19pm
1
I would like to search text in a div and put numbers in parenthesis in supertext.
So (1) would change to <sup>(1)</sup> but (xyz) would be left untouched.
I would also like to include commas in the supertext.
So (5),(8) would be converted to <sup>(5),(8)</sup>
Does anyone know how to do this?
Thank You E
Hi,
You can do it like this:
var p = document.querySelectorAll("p"),
myRegex = /(\\((\\d+)\\))/g;
for(var i=0; i < p.length; i++){
p[i].innerHTML = p[i].innerHTML.replace(myRegex, function(match) { return "<sup>" + match + "</sup>"; });
}
fiddle
eruna
April 29, 2014, 7:58pm
3
Thank you! Is there any way to include a comma in between the parenthesis. IE: (1),(2)?
So you want:
(1) foo bar (2) foo bar (3) foo bar
to become:
<sup>(1)</sup> foo bar, <sup>(2)</sup> foo bar, <sup>(3)</sup> foo bar
Is that correct?
eruna
April 29, 2014, 11:27pm
5
That would work, but actually only a comma would be safer. It would be less likely to trigger false responses.
Thank you!!!
Hi,
Then I have misunderstood what you are aiming for.
Could you give an example of normal text, then of what you would like it to become (as I do above).
eruna
April 30, 2014, 1:23pm
7
This is one I’m looking for:
Foo Bar (1) ,(2),(3) Foo Bar (1),(2)
to
Foo Bar <sup>(1) ,(2),(3)</sup> Foo Bar <sup>(1),(2)</sup>
Thank you!!!
Michael
eruna:
This is one I’m looking for:
Foo Bar (1) ,(2),(3) Foo Bar (1),(2)
to
Foo Bar <sup>(1) ,(2),(3)</sup> Foo Bar <sup>(1),(2)</sup>
Thank you!!!
Michael
Getting complicated.
This should work:
(\\((\\d+)\\))((\\s*,\\s*)?\\((\\d+)\\))*
http://regexpal.com/?flags=g®ex=(\((\d%2B)\ ))((%5Cs*%2C%5Cs*)%3F%5C((%5Cd%2B)%5C))*&input=Foo%20Bar%20(1)%20%2C(2)%2C(3)%0AFoo%20Bar%20(1)%2C(2)%20%0AFoo%20Bar%20(1)%0AFoo%20Bar%20(1)%20(2)%0AFoo%20Bar%20(1)%20%2C(2)%2C(3)%20Foo%20Bar%20(1)%20Foo%20Bar%20(1)%20(2)%20Foo%20Bar%20(1)%2C%20(2)
Looks messy though, it could probably be solved more elegantly.
myRegex = /(\\((\\d+)\\))((\\s*,\\s*)?\\((\\d+)\\))*/g;
I would do it like this:
<p>Foo Bar (1) ,(2),(3) Foo Bar (1),(2)</p>
var p = document.querySelectorAll("p"),
myRegexp = /\\([()0-9 ,]\\)/g;
for(var i=0; i < p.length; i++){
p[i].innerHTML = p[i].innerHTML.replace(myRegexp, function(match) { return "<sup>" + match + "</sup>"; });
}
fiddle
That does each (#) set individually and misses the comma.
regexpal
Oh yeah, well spotted.
The regex should be:
myRegexp = /\\([()0-9 ,]\\+)/g;
I forgot the plus to make it greedy.
fiddle
eruna
May 1, 2014, 1:52pm
12
Thanks guys!!!
One of these days I will master these regular expressions too.
E
eruna
May 1, 2014, 9:30pm
13
It worked! I just tried it out.