"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."
Verwijderd
[ Voor 19% gewijzigd door Verwijderd op 11-05-2005 21:23 ]
Dat snap ik niet - je bedoelt?Verwijderd schreef op woensdag 11 mei 2005 @ 21:22:
Doe dus een eenvoudige replace, desnoods met de regex /<\/?b[^>]*]>/g
1
2
3
4
| function strip_tags(string) { string = string.replace(/<\/?b[^>]*]>/g, string); alert(string); } |
Ook als de string bold-tags bevat, retourneert hij de hele string...
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."
en dit?Reveller schreef op woensdag 11 mei 2005 @ 21:43:
[...]
Dat snap ik niet - je bedoelt?
...
Ook als de string bold-tags bevat, retourneert hij de hele string...
1
2
3
4
| function strip_tags(string) { string = string.replace(/<\/?b[^>]*>/g, ''); alert(string); } |
Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <script> function bold(string) { if (!/<\/?b[^>]*>/g) { string = '<b>'+string+'</b>'; } else { string = string.replace(/<\/?b[^>]*>/g, ''); } alert(string); } </script> <form> <input type="text" name="text"> <input type="button" onclick="bold(document.forms[0].text.value);" value="toggle"> </form> |
Dit werkt alleen niet. Waarschijnlijk zit de fout in regel 3 en kun je niet zomaar op een regexp checken. Via welke weg kan ik ervoor zorgen dat het if statement controleer of de ingevoerde string omgeven is door bold-tags?
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."
1
2
3
| if (!/<\/?b[^>]*>/g.test(string)) { string = '<b>'+string+'</b>'; } |
Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."
1
2
3
4
5
6
| input | gewenste output: -------------------------+------------------------- <b>dit is een string</b> | dit is een string dit is een string<b> | dit is een string<b> dit<b>is een</b> string | dit <b>is een</b> string </b>dit is een string<b> | </b>dit is een string<b> |
Nu is de output bij alle voorbeelden "dit is een string". Hoe bepaal ik nu dat de bold tags alleen gestript worden als de string begint met < b> en eindigt met < /b>?
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."
1
| ^<b[^>]*>(.*?)</b>$ |
Volgens mij checkt deze regexp of de string begint met een bold-tag (eventueel met properties) en eindigt met een bold-tag. Probleem is alleen dat de volgende functie niet werkt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <script>
function bold(string) {
if (!^<b[^>]*>(.*?)</b>$.test(string)) {
string = '<b>'+string+'</b>';
}
else {
string = string.replace(^<b[^>]*>(.*?)</b>$, '');
}
alert(string);
}
</script>
<form>
<input type="text" name="text">
<input type="button" onclick="bold(document.forms[0].text.value);" value="toggle">
</form> |
De javascript Console van FF geeft een syntax error aan - wat is dan wel de correcte syntax? Als ik iets aan de systax verander (bv. regex met slash laten beginnen), klopt de regex toch niet meer?
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."
1
| string = string.replace(/^<b[^>]*>(.*?)</b>$/, ''); |
of als alternatief:
1
2
| var re = new RegExp('^<b[^>]*>(.*?)</b>$'); string = string.replace(re, ''); |
Intentionally left blank
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| <script>
function bold(string) {
var re = new RegExp('^<b[^>]*>(.*?)</b>$');
if (!re.test(string)) {
string = '<b>'+string+'</b>';
}
else {
string = string.replace(re, '');
}
alert(string);
}
</script>
<form>
<input type="text" name="text">
<input type="button" onclick="bold(document.forms[0].text.value);" value="toggle">
</form> |
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."
1
| string = string.replace(re, '$1'); |
Intentionally left blank