Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var text = "We are <mixcase>living</mixcase> in a <upcase>yellow submarine</upcase>." +
- " We <mixcase><upcase>don't</upcase></mixcase> have <lowcase>anything</lowcase> else.";
- var closeIndex = text.indexOf("</");
- while (closeIndex >= 0) {
- text = implementTags(text, closeIndex);
- closeIndex = text.indexOf("</");
- }
- console.log(text);
- function implementTags(str, closeStartIndex) {
- var tag, tagLen, indexOpenBracketStart, closeEndIndex, textToReplace;
- closeEndIndex = str.indexOf(">", closeStartIndex);
- tag = str.substring(closeStartIndex + 2, closeEndIndex);
- tagLen = tag.length;
- indexOpenBracketStart = (str.substring(0, closeStartIndex)).lastIndexOf("<" + tag + ">");
- textToReplace = str.substring(indexOpenBracketStart, closeEndIndex+1);
- return str.replace(textToReplace, textManipulation(textToReplace, tag, tagLen));
- }
- function textManipulation(text, tagType, len) {
- var replacementText = text.substring(len + 2, text.length - len - 3);
- switch (tagType) {
- case "upcase": replacementText = replacementText.toUpperCase(); break;
- case "lowcase": replacementText = replacementText.toLowerCase(); break;
- case "mixcase": replacementText = executeMixcase(replacementText); break;
- }
- return replacementText;
- }
- function executeMixcase(text) {
- var result = "",
- option = 0;
- for (var i = 0; i < text.length; i++) {
- option = Math.random();
- result += (option < 0.5 ? text[i].toLowerCase() : text[i].toUpperCase());
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement