Javascript在开头和结尾删除字符串

Javascript在开头和结尾删除字符串

本文介绍了Javascript在开头和结尾删除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于以下字符串

  ...这里.. 
..来...
.their.here。

如何删除字符串的开头和结尾,如删除所有空格的修剪,使用javascript



输出应为

 这里

他们。


解决方案

以下是此任务的RegEx为 /(^ \。+ | \。+ $)/ mg 的原因:


  1. /()/ 内写下模式您要在字符串中找到的子字符串:

    var x =colt.replace(/(ol)/,'a'); 会给你 x ==cat;


  2. ^ \。+ | \。+ $ in /()/ 由符号<$分为2部分c $ c> | [表示或]




    1. ^ \。+ 表示在开始时尽可能多地找到


    2. \。+ $ 表示尽可能多地找到

      var x = "colt".replace(/(ol)/, 'a'); will give you x == "cat";

    3. The ^\.+|\.+$ in /()/ is separated into 2 parts by the symbol | [means or]

      1. ^\.+ means to find as many . as possible at the start.

      2. \.+$ means to find as many . as possible at the end.

    4. The m behind /()/ is used to specify that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary.

    5. The g behind /()/ is used to perform a global match: so it find all matches rather than stopping after the first match.

    To learn more about RegEx you can check out this guide.

    这篇关于Javascript在开头和结尾删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:00