MozTW 討論區
https://forum.moztw.org/

請教一下關於修改字串的方法 [javascript]
https://forum.moztw.org/viewtopic.php?f=8&t=18770
1 頁 (共 1 頁)

發表人:  Merci chao [ 2007-04-29, 17:16 ]
文章主題 :  請教一下關於修改字串的方法 [javascript]

一個很看起來很笨的問題... :oops:

現在我有一字串:
代碼:
string = "first:10second:20last:30"

請問, 應該要用甚麼方法才能把中間的 20 改成 40 :?:
代碼:
string = "first:10second:40last:30"

* 當中的數字為自變量, 其餘均為固定字串 *

發表人:  passerby [ 2007-04-29, 18:33 ]
文章主題 : 

replace substring ???? :?: :?: :?:

example:
代碼:
/***************************************************************************************************/
var str = "AA BB CC DD EE, AA BB CC DD EE";
str = str.replace("AA", "11");         //use substring as parameter, replace only the first match
alert('parameter use substring:\n' + str);
/*
output:

parameter use substring:
11 BB CC DD EE, AA BB CC DD EE
*/
/***************************************************************************************************/

str = "AA BB CC DD EE, AA BB CC DD EE";
str = str.replace(/DD/g, "22");      //use regular expression, set replace flag to global, then replace all match
alert('parameter use regex:\n' + str);
/*
output:

parameter use regex:
AA BB CC 22 EE, AA BB CC 22 EE
*/
/***************************************************************************************************/

str = "AA BB CC DD EE, AA BB CC DD EE";
//g = global search
str = str.replace(/DD/g, "22");      //replace all match
alert('regex with flag global:\n' + str);
/*
output:

regex with flag gloabl:
AA BB CC 22 EE, AA BB CC 22 EE
*/
/***************************************************************************************************/

str = "AA BB CC DD EE, AA BB CC DD EE";
//without global flag
str = str.replace(/DD/, "22");      //replace the first match only
alert('regex without flag global:\n' + str);
/*
output:

regex without flag global:
AA BB CC 22 EE, AA BB CC DD EE
*/
/***************************************************************************************************/

// i = ignore case
str = "AA BB CC DD EE, aa BB CC DD EE";
str = str.replace(/aa/gi, "Apple");
alert('regex with flag ignore case:\n' + str);
/*
output:

regex with flag ignore case:
Apple BB CC DD EE, Apple BB CC DD EE
*/
/***************************************************************************************************/
// ignore case is not set
str = "AA BB CC DD EE, aa BB CC DD EE";
str = str.replace(/aa/g, "Apple");
alert('regex without flag ignore case:\n' + str);
/*
output:

regex without flag ignore case:
AA BB CC DD EE, Apple BB CC DD EE
*/
/***************************************************************************************************/
//m = multi-line
str = "AA BB CC DD EE" +  "\n"  + "AA BB CC DD EE";
str = str.replace(/^AA/gm, "Apple");      //replace string which is beginning of 'AA'
alert('regex with flag multi-line:\n' + str);
/*
output:

regex with flag multi-line:
Apple BB CC DD EE
Apple BB CC DD EE
*/
/***************************************************************************************************/
//if multi-line is not set
str = "AA BB CC DD EE" +  "\n"  + "AA BB CC DD EE";
str = str.replace(/^AA/g, "Apple");
alert('regex without flag multi-line:\n' + str);

/*
output:

regex without flag multi-line:
Apple BB CC DD EE
AA BB CC DD EE
*/
/***************************************************************************************************/


refs:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:replace

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:RegExp

發表人:  Merci chao [ 2007-04-29, 20:48 ]
文章主題 : 

有點眼花....@@"
大概明白原理吧, 不過看來是我表達得不清楚...

我的想法是這樣的:
代碼:
var string = "first:10second:20last:30";

// 未知方法...
...............
...............
...............
// 把字串分割, 得到如下兩部份:
// string1 = "first:10second:";
// string2 = "last:30";

string = string1 + 40 + string2; // 與新數值合併
其實我忘記了說當中的數字是自變量來的 :oops:

發表人:  yuoo2k [ 2007-04-29, 21:31 ]
文章主題 : 

var s = "first:10second:20last:30";
var m = s.match(/^first:(\d+)second:(\d+)last:(\d+)$/);
m[1] //10
m[2] //20
m[3] //30

發表人:  passerby [ 2007-04-30, 02:01 ]
文章主題 : 

yuoo2k 寫:
var s = "first:10second:20last:30";
var m = s.match(/^first:(\d+)second:(\d+)last:(\d+)$/);
m[1] //10
m[2] //20
m[3] //30


我本來想起的是 string.split,不過看來yuoo2k
建議的 string.match 是更好的選擇,因為他可以方便處理 no match 的情況。

code大約如下
代碼:
var s = "first:10second:20last:30";
var re = /^first:(\d+)second:(\d+)last:(\d+)$/;

var m = s.match(re);

if (m != null){     
   m[2] = 40;
   var str = "first:" + m[1] + "second:" + m[2] + "last:" + m[3];             
   alert(str);      //first:10second:40last:30
}


另外,留意一下
string.match 的 regex parameter使用 g ,傳回的會是array of all matches

string.match 的 regex parameter 沒有使用 g ,傳回的會是array of all submatches。
代碼:
//regular expression 使用 g (global)後,string.match就不會傳回 submatch

var s = "first:10second:20last:30,first:40second:50last:60";

var re = /first:(\d+)second:(\d+)last:(\d+)/g;
var m = s.match(re);

if (m != null){
   alert('m[0]:' + m[0]);  //m[0]:first:10second:20last:30
   alert('m[1]:' + m[1]);  //m[1]:first:40second:50last:60
}


代碼:
// 使用 regex.exec(string)例子,目的是取得regex submatch string

var s = "first:10second:20last:30,first:40second:50last:60";

var re = /first:(\d+)second:(\d+)last:(\d+)/g;
var m = re.exec(s);

if (m != null){
   alert(m[0]);  //first:10second:20last:30
   alert(m[1]);  //10
   alert(m[2]);  //20
   alert(m[3]);  //30
}

m = re.exec(s);   //注意這裡會從 Last match position開始找,而不是重頭開始找

if (m != null){
   alert(m[0]);  //first:40second:50last:60
   alert(m[1]);  //40
   alert(m[2]);  //50
   alert(m[3]);  //60
}





順便還想說一下,雖然有一點離題,string.replace的第二個 parameter不一定是 constant string,也可以是 function (javascript 1.3上支援),如果只想將 substring作某些處理,再放回原來 string中, 這時使用 function variable就更方便。

e.g.
string.replace(regexp/substr, newSubStr/function[, flags])

代碼:
//example:
var s = "first:10second:20last:30,first:40second:50last:60";
var re = /(\d+)/g;

var f = function(x){
   x = parseInt(x);
   x = x + 10;
   return x;
};

s = s.replace(re, f);
alert(s);   //first:20second:30last:40,first:50second:60last:70


refs:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:replace

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:match

發表人:  Merci chao [ 2007-04-30, 17:27 ]
文章主題 : 

謝謝兩位的解答 :D
看來有空應該要學一學基礎的東西了 =="

想問一問, 以下紅色部份的意思分別是甚麼 :?:
引言回覆:
/^first:(\d+)second:(\d+)last:(\d+)$/

發表人:  legnaleurc [ 2007-04-30, 18:13 ]
文章主題 : 

Merci chao 寫:
謝謝兩位的解答 :D
看來有空應該要學一學基礎的東西了 =="

想問一問, 以下紅色部份的意思分別是甚麼 :?:
引言回覆:
/^first:(\d+)second:(\d+)last:(\d+)$/


JavaScript的正則表達式會放在兩個/中間
所以會有/blahblah/就代表這是一個正則表達式
而^代表的是行首匹配,$代表行尾匹配
\d對任一個數字做匹配(與[0-9]同義)
+代表前面的匹配至少出現一次以上(與{1,}同義)

詳細可以參照wikipedia的說明

發表人:  kourge [ 2007-05-02, 14:09 ]
文章主題 : 

Merci chao 寫:
謝謝兩位的解答 :D
看來有空應該要學一學基礎的東西了 =="

想問一問, 以下紅色部份的意思分別是甚麼 :?:
引言回覆:
/^first:(\d+)second:(\d+)last:(\d+)$/

/ 是開始 RegExp,^ 匹配行首,\d 代表任意一個數字(同 [0-9]),括號是「記憶群組」(會出現在傳回的陣列),+ 代表至少要出現一次或者以上(同 {1,}),$ 匹配行尾,/ 是結束 RegExp。

順帶一提:
+ 跟 {1,} 一樣,一次或者以上
* 是 {0,},就是所謂「可有可無也可重複」

(x) 會把整個表達式群組起來記憶匹配並會在陣列傳回
(?:x) 有群組效果,但沒有記憶效果
群組很有用,比如說 (ab)+ 會批配 ababab

1 頁 (共 1 頁) 所有顯示的時間為 UTC + 8 小時
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/