当前位置:网站首页>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>
边栏推荐
- CEPH distributed storage performance tuning (6)
- Multi activity disaster recovery construction after station B 713 accident | takintalks share
- Mixin\ plug in \scoped style
- 20210519 leetcode double pointer
- When the script runs in the background, it redirects the log from the console to its own named file
- Chapter 13 IO flow
- 2022 global Vocational Education Industry Development Report
- BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
- Redistemplate cannot get the value according to the key
- [database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported
猜你喜欢

SQL question brushing: find out the current salary of all employees

Basic architecture of data Lake

The song of the virtual idol was originally generated in this way!

20210519 leetcode double pointer

Fundamentals of mathematics 01

Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling

详述try-catch-finally

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.

概述有名内部类与匿名内部类

Chapter 10 enumeration classes and annotations
随机推荐
隔离级别
Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
POJ1988_Cube Stacking
What are metauniverse and NFT digital collections?
js真伪数组转换
Multi activity disaster recovery construction after station B 713 accident | takintalks share
20210422 consolidation interval
Why does MySQL index use b+ tree instead of jump table?
Plus版SBOM:流水线物料清单PBOM
US pressure surges tiktok changes global safety director
详述HashSet的add方法
20210519 leetcode double pointer
Detailed explanation of flask framework
MySQL扩展
评价自动化测试优劣的隐性指标
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 (
[excerpt] [medical image] common DICOM thumbnail interpretation and viewer converter conversion tool
5V boost 9V chip
概述静态内部类与非静态内部类
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi