正則匹配:去除字串中的空格,獲取select下拉選項中的option的文字內容
阿新 • • 發佈:2019-02-05
<pre name="code" class="javascript">//去除空格 String.prototype.Trim = function() { return this.replace(/\s+/g, ""); } //去除換行 function ClearBr(key) { key = key.replace(/<\/?.+?>/g,""); key = key.replace(/[\r\n]/g, ""); return key; } //去除左側空格 function LTrim(str) { return str.replace(/^\s*/g,""); } //去右空格 function RTrim(str) { return str.replace(/\s*$/g,""); } //去掉字串兩端的空格 function trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } //去除字串中間空格 function CTim(str) { return str.replace(/\s/g,''); } //是否為由數字組成的字串 function is_digitals(str) { var reg=/^[0-9]*$/; //匹配整數 return reg.test(str); }
//獲取select下拉選項中的option的文字內容
<pre name="code" class="javascript">var obj = document.getElementById("company");
var company = obj.options[obj.selectedIndex].text;