Created
April 24, 2016 16:31
-
-
Save anonymous/d1b58c0042a36e41ce3d23680f836623 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/pivomoxeca
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id="log"></div> | |
<script> | |
var str = '朝(ちょう)三(さん)暮(ぼ)四(し)'; | |
function parse(curosr) { | |
var start = str.indexOf('(', curosr); | |
var end = str.indexOf(')', start); | |
if(start != -1 && end != -1) { | |
console.log(start); | |
console.log(end); | |
console.log(str.substring(start+1, end)); | |
parse(end); | |
} | |
} | |
function parse2(s) { | |
s = s.replace(/(/g, '<rt>'); | |
s = s.replace(/)/g, '</rt>'); | |
var _html = '<ruby>' + s + '</ruby>'; | |
console.log(_html); | |
if(validRubyText(_html)) { | |
document.getElementById('log').innerHTML = _html; | |
} else { | |
document.getElementById('log').innerHTML = 'die'; | |
} | |
} | |
function validRubyText(text) { | |
var re = new RegExp(['ruby', 'rt'].map(function (el) { | |
var openTag = '<' + el + '\\b[^>]*>'; | |
var closeTag = '<\/' + el + '\\b[^>]*>'; | |
return openTag + '[\\s\\S]*' + closeTag; | |
}).join('|'), 'i'); | |
return re.test(text); | |
} | |
parse2(str); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment