怎么用js提取字符串中的某一段字符 js 正则表达式提取某一段字符
\u5982\u4f55\u7528js\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d(url)\u7684\u67d0\u4e00\u6bb5\u5b57\u7b26\u5176\u5b9e\u6bd4\u8f83\u7b80\u5355\uff0c\u5206\u4eab\u4e00\u6bb5\u6211\u4e4b\u524d\u5199\u7684js\u63d0\u53d6URL\u53c2\u6570\u7684\u51fd\u6570\u5427
function UrlParamHash(url) { var params = [], h; var hash = url.slice(url.indexOf("?") + 1).split('&'); for (var i = 0; i < hash.length; i++) { h = hash[i].split("="); params.push(h[0]); params[h[0]] = h[1]; } return params;}// \u5982\u4e0b\u4f7f\u7528, b\u5c31\u662f\u4f60\u7684URL\u5b57\u7b26\u4e32var params = UrlParamHash(b), id = params['id'];\u4e0d\u8fc7\u5199\u5b8c\u53d1\u73b0\u4f60\u7684URL\u4e0d\u662f\u5f88\u6807\u51c6\uff0c\u56e0\u4e3a\u4e00\u822c\u5730\u5740\u548c\u53c2\u6570\u76f4\u63a5\u662f\u7528"?"\u5206\u5272\u7684\uff0c\u5982\u679c\u4f60\u7684\u4e0d\u662f\u7684\u8bdd
\u4f60\u53ef\u4ee5\u628a\u51fd\u6570\u91ccindexOf("?")\u6839\u636e\u4f60\u7684\u57df\u540d\u6362\u6210indexOf("com/")\u4e4b\u7c7b\u7684
\u4e5f\u53ef\u4ee5\u6839\u636e\u4f60\u5177\u4f53\u7684URL\u89c4\u5219\u4fee\u6539\u6574\u4e2a\u51fd\u6570\uff0c\u53cd\u6b63\u539f\u7406\u5c31\u5728\u8fd9\u91cc
var str = "BEGIN CONST a,b=10,c END IF a>10 && (a+cc) THEN RETURN true END ELSE RETURN false END END END";str=str.replace(/\s/g,"");//\u53d6\u6389\u7a7a\u683cstr=str.match(/IF(\S*)THEN/)[1];//\u6b63\u5219\u53d6\u51faIF\u548cTHEN\u4e4b\u95f4\u7684\u5b57\u7b26\u4e32str=str.split("&&");//\u6309\u7167&&\u5206\u7ec4var str_end=str[0].split('>'); //\u6309\u7167>\u5206\u7ec4alert(str_end);//\u6700\u7ec8\u7ed3\u679c\u4e3astr_end[0]=a str_end[1]=10 ;
String.prototype.getQuery = function(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = this.substr(this.indexOf("\?")+1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}
var s = "web1.aspx?status=item&mode=edit";
alert(s.getQuery("status"));
alert(s.getQuery("mode"));
String.prototype.getQuery = function(name)
的意思是为String对象增加一个getQuery(name)方法.
var url ='?id=632573?id=79323&ed=7947';
var str = url.substr(1);
strs = str.split("?");
var theRequest = new Object();
theRequest[strs[0].split("=")[0]]=unescape(strs[0].split("=")[1]);
var id = theRequest['id']; //取得ID
alert(id);
以上仅解决你这个例子的情况,以为大多数情况下URL参数的键名都不是一样的
而且开头一个?中间都是& 所以实际可以一次性获取全部的参数的
//这个是正常的URL注意id和id2键名不一样中间都是&间隔
var url ='?id=632573&id2=79323&ed=7947';
var str = url.substr(1); //去掉第一个?
strs = str.split("&");//以&分割
var theRequest = new Object();
//取得全部并赋值
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
//输出
var id = theRequest['id']; //取得ID
var id2 = theRequest['id2']; //取得ID2
var ed = theRequest['ed']; //取得ed
alert(id);
alert(id2);
alert(ed);
答案一:"?id=632573&id=79323?ed=7947".replace(/^.*?id=(\d+).*$/,'$1')
答案二:"?id=632573&id=79323?ed=7947".replace(/^.*?(\d+).*$/,'$1')
测试通过的~请采纳~不明白的可以继续追问~
看到楼上好多人都写了,我也写了一个,希望能帮到你:
var b = "rwewerwer?id=632573&id=79323?ed=7947".match(/(\d+)/g)[0];
var str = "?id=632573&id=79323?ed=7947";
var s = str.indexOf('=')+1;//第一个=出现的位置
var e = str.indexOf('&');//第一个&出现的位置
var re = str.substring(s,e);//返回的值
绛旓細缁撹锛欽avaScript涓殑match鏂规硶鍙互鐢ㄦ潵鎴彇瀛楃涓蹭腑鎸囧畾瀛楃涓蹭箣闂寸殑鍐呭锛屽渚嬪瓙涓殑 "" 鍜 "" 涔嬮棿鐨勫唴瀹广傞氳繃姝e垯琛ㄨ揪寮 `/(\S*)/`锛屾垜浠彲浠ユ壘鍒板苟鎻愬彇鍑鸿繖閮ㄥ垎鍐呭銆俙\S*` 琛ㄧず鍖归厤闆朵釜鎴栧涓潪绌虹櫧瀛楃锛宍*` 绗﹀彿浣垮叾鍙互閲嶅鍑虹幇銆俶atch鏂规硶浼氳繑鍥炲尮閰嶇殑缁撴灉锛屽鏋滄病鏈夋壘鍒板尮閰嶏紝鍒欒繑鍥瀗...
绛旓細2. `match`鏂规硶鏄疛avaScript涓瓧绗︿覆瀵硅薄鐨勪竴涓柟娉曪紝瀹冭繑鍥炰竴涓暟缁勶紝璇ユ暟缁勫寘鍚簡涓庢鍒欒〃杈惧紡鍖归厤鐨勬枃鏈墖娈銆傛垜浠彲浠ュ埄鐢ㄨ繖涓柟娉曟壘鍒版寚瀹氱殑瀛楃涓叉ā寮忋傚叿浣撴搷浣滄楠ゅ涓嬶細1. 瀹氫箟浣犵殑鐩爣瀛楃涓插拰瑕佹煡鎵剧殑涓や釜鎸囧畾瀛楃涓层備緥濡傦紝浣犳兂浠庡瓧绗︿覆`"Hello, world! This is a test string."`涓埅鍙朻...
绛旓細3銆佽鎻愬彇瀛愬瓧绗︿覆鐨勫瓧绗︿覆鏂囧瓧鎴 String 瀵硅薄銆4銆乻tart蹇呴夐」銆5銆佹墍闇鐨勫瓙瀛楃涓茬殑璧峰浣嶇疆銆6銆瀛楃涓蹭腑鐨绗竴涓瓧绗︾殑绱㈠紩涓 0銆7銆乴ength鍙夐」銆8銆佸湪杩斿洖鐨勫瓙瀛楃涓蹭腑搴斿寘鎷殑瀛楃涓暟銆9銆佽鏄庡鏋 length 涓 0 鎴栬礋鏁帮紝灏嗚繑鍥炰竴涓┖瀛楃涓层10銆佸鏋滄病鏈夋寚瀹氳鍙傛暟锛屽垯瀛愬瓧绗︿覆灏嗗欢...
绛旓細String.prototype.getQuery = function(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");var r = this.substr(this.indexOf("\?")+1).match(reg);if (r!=null) return unescape(r[2]); return null;} var s = "web1.aspx?status=item&mode=edit";alert(...
绛旓細substring(): 鎴戜滑鍙互閫氳繃鎸囧畾寮濮嬪拰缁撴潫浣嶇疆鎻愬彇瀛楃涓茬殑涓閮ㄥ垎锛岀敓鎴愪竴涓柊鐨勫瓙涓层俽eplace(): 閫氳繃姝e垯琛ㄨ揪寮忔煡鎵惧尮閰嶉」锛岀劧鍚庣敤鏂扮殑瀛楃涓叉浛鎹㈣繖浜涘尮閰嶉儴鍒嗐俿earch(): 鍦ㄥ瓧绗︿覆涓鎵ц姝e垯琛ㄨ揪寮忕殑鎼滅储锛屾壘鍒板尮閰嶆椂杩斿洖鍖归厤浣嶇疆锛屾壘涓嶅埌鏃惰繑鍥-1銆俿lice(): 绫讳技substring锛屼絾鏇寸伒娲伙紝鍙互鎺ュ彈璐熸暟绱㈠紩...
绛旓細瀛楃涓鎴彇甯哥敤鐨勫嚑绉嶆柟娉曪細start: 蹇呴渶銆傞潪璐熺殑鏁存暟锛岋紙绗竴涓厓绱犵殑绱㈠紩涓 0锛夈俿top: 鍙夈備竴涓潪璐熺殑鏁存暟锛屾瘮瑕鎻愬彇鐨勫瓙涓茬殑鏈鍚庝竴涓瀛楃鍦 stringObject 涓殑浣嶇疆澶 1銆傚鏋滀笉浼狅紝閭d箞杩斿洖鍒板瓧绗︿覆鐨勭粨灏俱 let str = 'helloword';str.substring(1,5); // ...
绛旓細璇锋暀涓涓JS姝e垯鎻愬彇缂栫爜鍚嶶rl涓殑涓娈靛瓧绗︿覆銆 20 瀛楃涓蹭负:sn=b27de7a6f201b50c9cf65db582b5d913&idx=2&scene=0&title=%E3%80%90%E7%88%B1%E6%97%85%E6%B8%B8%E3%80%91%E3%80%90%E6%AC%A7%E6%B4%B2%E4%B8%83%E8%89%B2%E3%80%91%E5%B... 瀛楃涓蹭负:sn=b27de7a6f201b50c9cf65db...
绛旓細var str = "BEGIN CONST a,b=10,c END IF a>10 && (a+c<20 || b>c) THEN RETURN true END ELSE RETURN false END END END";str=str.replace(/\s/g,"");//鍙栨帀绌烘牸str=str.match(/IF(\S*)THEN/)[1];//姝e垯鍙栧嚭IF鍜孴HEN涔嬮棿鐨瀛楃涓str=str.split("&&");//鎸夌収&&鍒嗙粍...
绛旓細鍏跺疄姣旇緝绠鍗曪紝鍒嗕韩涓娈鎴戜箣鍓嶅啓鐨js鎻愬彇URL鍙傛暟鐨勫嚱鏁板惂 function UrlParamHash(url) { var params = [], h; var hash = url.slice(url.indexOf("?") + 1).split('&'); for (var i = 0; i < hash.length; i++) { h = hash[i].split("="); params.push(h...
绛旓細IE8+,鍙婂叾浠栨祻瑙堝櫒鍙互鐢 document.querySelector('meta[name=description]').getAttribute('content')IE浣庣増鏈敤getElementsByTagName锛岀劧鍚庡惊鐜壘涓 浣跨敤JQuery鍙互鐩存帴鍏煎 alert($('meta[name=description]').attr('content'));