標籤:表達 操作 pwd use path 正則表達 sage 方法 file
一,js Regex
test 判斷制度串是否符合規定的正則
(1)定義Regex匹配規則
js 中定義Regex為rep=/\d+/,兩個//之間為正則模式
(2)rep.test("assdsda89sdasdas") ,返回true,一般test 方法為只要字串中的包含正則模式即返回true
(3)rep=/^\d+$/完全符合正則模式
exec 擷取匹配的資料
1, (1)rep=/\d+/;
(2)str="wangshen_67_houyanfa_20"
(3)rep.exec(str) 返回["67"]
2, js exec 分組匹配
text="JavaScript is more fun than Java or JavaBeans!"var pattern = /\bJava(\w*)\b/; pattern.exec(text) #返回 ["JavaScript", "Script"]
3,js exec 全域匹配
text="JavaScript is more fun than Java or JavaBeans!"var pattern = /\bJava\w*\b/g;pattern.exec(text)# ["JavaScript"]pattern.exec(text)# ["Java"]pattern.exec(text)# ["JavaBeans"]pattern.exec(text)# nullpattern.exec(text) #加g表示全域匹配,匹配一個輸出一個,當全部匹配完成時輸出null 再匹配從頭開始
4,js exec 全域加分組匹配
JavaScript is more fun than Java or JavaBeans!var pattern = /\bJava(\w*)\b/g;# ["JavaScript",‘Script‘]# ["Java", ""]# ["JavaBeans", "Beans"]# null #分組匹配會對匹配到的結果再進行一次匹配
5,其它匹配模式
(1)/.../i 不區分大小寫
(2)/.../m 表示多行匹配,js 中預設支援多行匹配,也就是單獨加g也可以完成多行匹配,但是在匹配模式中^$,匹配多行時需要m參數
例如
text="JavaScript is more fun than \n Java or JavaBeans!"var pattern = /\bJava(\w*)\b/g;pattern.exec(text) #返回["JavaScript",‘Script‘] 也可以匹配成功,表示預設支援多行text="JavaScript is more fun than \n Java or JavaBeans!"var pattern = /^Java(\w*)/g; #匹配以Java開頭的字串,且後面為任一字元pattern.exec(text) #返回["JavaScript",‘Script‘] pattern.exec(text) #返回null var pattern = /^Java(\w*)/gm; pattern.exec(text) #返回["JavaScript",‘Script‘] pattern.exec(text) #返回["Java",""]
6,a標籤綁定事件
<a onclick=‘return Func();‘>asdf</a>function Func(){ return false: #利用DOM綁定方式增加a標籤事件}<a>asdf</a>$(‘a‘).click(function(){return false;}) #利用jquery方式綁定事件
7,form 表單提交事件
<form><input type=‘text‘ /><input type=‘password‘ /><input type=‘submit‘ /></form> $(‘:submit‘).click(function(){ $(‘:text,:password‘).each(function(){...return false;})return false;})
8,標籤定義事件執行順序
像a input submit 標籤一般預設都為自訂事件先執行,checkbox 預設事件先執行
9,css設定重要性
<style> .no-radus{ border-radius:0 !important;}</style>
二,django
1,django 設定靜態檔案路徑
在setting.py中最下面添加添加以下代碼
STATIC_URL = ‘/static/‘
STATICFILES_DIRS = { os.path.join(BASE_DIR,‘static‘),}
2,建立完django project 後的操作
(1),配置模板的路徑
TEMPLATES = [{‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)],‘APP_DIRS‘: True,‘OPTIONS‘: {‘context_processors‘: [‘django.template.context_processors.debug‘,‘django.template.context_processors.request‘,‘django.contrib.auth.context_processors.auth‘,‘django.contrib.messages.context_processors.messages‘,],},},]
(2),配置靜態檔案的路徑
#在 setting.py 中添加STATICFILES_DIRS = (os.path.join(BASE_DIR, ‘static‘),)#在html中添加<link rel="stylesheet" href="/static/commons.css" />
3,view.py擷取使用者提交的資料
if request.method == "post": user = request.POST.get(‘user‘,None) pwd = request.POST.get(‘pwd‘,None) #擷取使用者提交的資料中 即使不存在相應的值也不會報錯 user = request.POST[‘user‘] pwd = request.POST[‘pwd‘] #擷取使用者提交的資料。如果不存在就會報錯
4,view.py中函數處理重新導向到其它網址
from django.shortcuts import redirect #匯入模組return redirect(‘http://www.baidu.com‘) #在view函數中添加,重新導向到其它網站
from django.shortcuts import render
return render(request,‘login.html‘) #找到本地的模板,開啟html檔案
5,render 返回錯誤資訊
return render(request,‘login.html‘,{‘error_msg‘:error_msg})
6,django 處理html模板for 迴圈與取某一個值
{% for row in user_list %} <tr> <td>{{ row.username }}</td> <td>{{ row.gender }}</td> <td>{{ row.email}}</td> </tr>{ %endfor %} #部分html代碼def home(request): return render(request,‘home.html‘,{‘user_list‘: USER_LIST}} #部分views.py代碼
7,css input 框中提示字設定
<input type="text" name="username" placeholder="使用者名稱" />
8,django 已get方式擷取值,需要在url中增加值
例如:
http://127.0.0.1:8009/home?nid=123&name=jack #get方式請求#views函數中print(request.GET) #返回<QueryDict:{‘name‘:[alex],‘nid‘:[‘123‘]}> print(request.GET.get(‘nid‘))#返回123
Python 學習第十八天 js 正則及其它前端知識