Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js format用法 #47

Open
lcgyh opened this issue Mar 29, 2018 · 0 comments
Open

js format用法 #47

lcgyh opened this issue Mar 29, 2018 · 0 comments

Comments

@lcgyh
Copy link
Owner

lcgyh commented Mar 29, 2018

var code='123'
 	var frstcode='ph001'
 	var aftercode='ph002'
 	var str='修改商品编码%s由%s改为%s'
 	
 	String.prototype.format = function() {  
	 	if(arguments.length == 0) return this;  
	 	var param = arguments[0];  
	 	var s = this;  
	 	if(typeof(param) == 'object') {  
	  	for(var key in param)  
	   		s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);  
	  		return s;  
	 	} else {  
			for(var i = 0; i < arguments.length; i++){
			  	s = s.replace(new RegExp("%s"), arguments[i]); 
			}  
	   		return s;  
	 	}  
	}

	var str1=str.format(code,frstcode,aftercode)
	console.log(str1)   //修改商品编码123由ph001改为ph002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant