当前位置:网站首页>Regular expressions remove spaces at both ends
Regular expressions remove spaces at both ends
2022-07-27 12:38:00 【ray_ zzzzz】
Use regular substitution to remove the spaces before and after the string , Because some lower version browsers do not support trim() Method , So I made a judgment
<script type="javascript">
let str = ' a i ldt ldt ';
let newStr = function(str){
if(str.trim){
// Some lower version browsers do not support this method , So judge first
return str.trim();
}
else{
// If the above method is not supported , Use regular
return str.replace(/^\s+|\s+$/g,"")
}
}
console.log(newStr(str)); //a i ldt ldt
</script>
To remove the space in the middle , But write like this
trim() Method cannot remove the middle space
<script>
let str = ' a i ldt ldt ';
let newStr = function(str){
return str.replace(/^\s+|\s+|\s+$/g,"")
}
console.log(newStr(str)); //aildtldt
</script>
边栏推荐
- Insert sort summary
- CEPH distributed storage performance tuning (6)
- Log4j2.xml configuration details
- [excerpt] [medical image] common DICOM thumbnail interpretation and viewer converter conversion tool
- Finally, I was ranked first in the content ranking in the professional field. I haven't been tired in vain during this period. Thanks to CSDN's official platform, I'm lucky and bitter.
- 堆
- Wechat applet session holding
- Dominoes staged: the beginning and end of the three arrow capital crash
- 20210419 combined sum
- Detailed explanation of flask framework
猜你喜欢

虚拟偶像的歌声原来是这样生成的!

5V boost 9V chip

BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (

Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning

II. Analysis of the execution process of make menuconfig

Self built personalized automatic quotation system to cope with changeable quotation mode

Chapter 10 enumeration classes and annotations

隔离级别

Mixin\ plug in \scoped style

20210519 leetcode double pointer
随机推荐
Top 10 international NFT exchanges
What should I do if I can't see any tiles on SAP Fiori launchpad?
I/O实例操作
Interviewer: how can you close an order without using a scheduled task?
js真伪数组转换
Several rounds of SQL queries in a database
关于2022年3月9日之后Typora登录不了--已解决
Alibaba cloud RDS exception during pool initialization
Watermelon Book + pumpkin book chapter 1-2
Keil mdk5.37 and above can add AC5 (armcc) compiler by themselves
Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
Error: slf4j: class path contains multiple slf4j bindings
Chapter 10 enumeration classes and annotations
多表查询
Laboratory procedures and references of chloramphenicol acetate
Necessary foundation: Signature Verification
Plus SBOM: assembly line BOM pbom
POJ1611_The Suspects
Chapter 12 generics
为什么需要外键?