我的JavaScript在线文本处理工具

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<textarea rows="5" id="output" style="width:99%;margin-right:auto"></textarea>
<br>
<input type="button" value="变成大写" onclick="daxie()" />&nbsp;
<input type="button" value="变成小写" onclick="xiaoxie()" />&nbsp;
<input type="button" value="生成c语言头文件声明" onclick="cyuyantouwenjianshengming()" />&nbsp;
<input type="button" value="清空输入框" onclick="clean()" />&nbsp;
<input type="button" value="html空格转移符" onclick="HTMLSpase()">&nbsp;
<input type="button" value="转成markdown表格" onclick="mdTableCopy()">&nbsp;
<input type="button" value="正则 java多行注释" onclick="java_annotation()">&nbsp;
<script>
function java_annotation() {
var java_java_doc_annotation = "\\s*(?:\\/\\*{1,2}|\\*\\/|\\*)";
var output = document.getElementById("output");
output.value = java_java_doc_annotation;
copy();
}
function mdTableCopy() {
var input = document.getElementById("output");
var str = input.value;
//删除多余的空白行
str = str.replace(/^\s*$(\n|\r\n)/mg, "");
//在每行行首行尾添加表格分割符竖杠‘|’
str = str.replace(/^(.+)$/mg, "|$1|");
var result = "";
//按行分割,因为我不知道正则匹配怎么写
var array = str.split("\n");
var line = "";
var count = 0;
for (var j = 0, length = array.length; j < length; j++) {
line = array[j];
count++
//在第一行的后面添加markdown表格对齐符号
if (count == 1) {
// console.log("-->"+line);
//获取列数
var cols = line.match(/\S+/g).length;
line = line.replace(/\s+/g, "|");
result += line + "\n";
//生成markdown表格对齐符号
for (var i = 0; i < cols; i++) {
result += "|:---";
}
result += "|\n";
}
//行中的空白符替换为表格分割符
line = line.replace(/\s+/g, "|");
//
if (j < length - 1) {
result += line + "\n";
}
//最后一样不加换行符
else {
result += line;
}
}
//写回文本域
input.value = result;
//复制到剪贴板
copy();
}
function daxie() {
var input = document.getElementById("output");
if (input.value == null || input.value == "") {
alert("请先输入");
return;
}
input.value = input.value.toUpperCase();//变成大写
copy()//复制到剪贴板中
}
function xiaoxie() {
var input = document.getElementById("output");
if (input.value == null || input.value == "") {
alert("请先输入");
return;
}
input.value = input.value.toLowerCase();//变成小写
copy();//复制到剪贴板中
}
function HTMLSpase() {
//&nbsp;
document.getElementById("output").value = "&nbsp;";
copy();
}
function clean() {
document.getElementById("output").value = "";
}
//复制方法
function copy() {
var in_output = document.getElementById("output");
in_output.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
if (confirm("代码已经复制到剪贴板粘贴即可")) {
in_output.value = "";
}
}
function cyuyantouwenjianshengming() {
var input = document.getElementById("output");
var oldValue = input.value;
if (oldValue == null || oldValue == "") {
alert("请先输入头文件名(一个单词)");
return;
}
var isOneWord = /^\w+$/;
if (isOneWord.test(oldValue)) {
var daxie = input.value.toUpperCase();//变成大写
input.value = "//" + oldValue + ".h\n" +
"#ifndef _" + daxie + "_H_ //如果没有引入头文件" + oldValue + ".h\n" +
" #define _" + daxie + "_H_ //那就引入头文件" + oldValue + ".h\n" +
"#endif";
copy();
}
else if (confirm("输入格式错误,是否清空输入框"))
document.getElementById("output").value = "";
}
</script>

参考链接