1 get-element-by-id
var s1 = "get-element-by-id"; 給定這樣一個連字符串,寫一個function轉換爲駝峯命名法形式的字符串 getElementById?
java
@Test
public void test1(){
String s = "get-element-by-id";
Pattern p = Pattern.compile("-(.)");
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
System.out.println(sb.toString());
}
javascript
var f = function(s) {
return s.replace(/-\w/g, function(x) {
return x.slice(1).toUpperCase();
})
}
2千位分隔符
java
@Test
public void test2(){
String s = "123123123";
System.out.println(s.replaceAll("(?=(\\B\\d{3})+$)", ","));
}
javascript
function format(number) {
var regx = /(?=(\B\d{3})+$)/g;
return (number + '').replace(regx, ',')
}
console.log("" + format("123123123123123") + "")
說明:
- 從右往左匹配,表示結尾的$要用;
- 每3個數字,一次或者多次 \d{3}+
- /(?=(\d{3})+$)/g 這個就是配置每3個數字前面的空字符‘’;
- 以上一個出現3倍數時,會多一個逗號,加上非邊界符\B;
function format2(number) {
var regx = /\d{1,3}(?=(\d{3})+$)/g;
return (number + '').replace(regx, '$&,') // $&表示與regx相匹配的字符串
}
console.log("" + format2("123123123123123") + "")
說明
- 這個是匹配後面有3個數字的{1,3}個數字; 所以替換時,得要用$&
3、獲取 url 參數
獲取 url 中的參數
- 指定參數名稱,返回該參數的值 或者 空字符串
- 不指定參數名稱,返回全部的參數對象 或者 {}
- 如果存在多個同名參數,則返回數組
java
@Test
public void testUrl(){
String s = "aaa?a=1&b=2&c=&d=4&b=66";
Pattern p = Pattern.compile("\\??(\\w+)=(\\w*)&?");
Matcher m = p.matcher(s);
HashMap<String,Object> map = new HashMap<>();
while(m.find()){
String name = m.group(1);
String value = m.group(2);
Object oldValue = map.get(name);
if(null == oldValue){
map.put(name,value);
}else{
if(oldValue instanceof List){
((List) oldValue).add(value);
map.put(name,oldValue);
}else{
List<String> list = new ArrayList<>();
list.add((String)oldValue);
list.add(value);
map.put(name,list);
}
}
}
System.out.println(map);
}
javascript
function getUrlParam(url, key) {
var arr = {};
var reg =/\??(\w+)=(\w*)&?/g;
url.replace(reg,function(m,matchKey,matchValue){
if (!arr[matchKey]) {
arr[matchKey] = matchValue;
} else {
var temp = arr[matchKey];
arr[matchKey] = [].concat(temp, matchValue);
}
});
console.log(arr)
}
getUrlParam('?a=1&b=3&c=&b=44')
4漢字
var regx = /^[\u4e00-\u9fa5]{0,}$/;
這是基本漢字,兩萬多個
參考:
漢字編碼範圍
https://www.qqxiuzi.cn/zh/hanzi-unicode-bianma.php
基本漢字編碼表
https://www.cnblogs.com/whiteyun/archive/2010/07/06/1772218.html