繁体   English   中英

使用 javascript/jquery 删除字符串的开头

[英]Remove beginning of String with javascript/jquery

我正在从一种方法中获得不断变化的结果。 它每次都是一个字符串,但在每个字符串的开头都写着“String(xxx) “字符串的文本... ”。xxx 取决于字符串内部的字符数。我尝试使用正则表达式来删除所有内容在 " 首次出现之前:

(result.replace(/^.+?(\\") /, ''))

但我不确定我是否正确使用它。 我也会接受其他方法,在每种情况下都删除字符串的开头(与字符串的长度无关)。

例子:

return String: 'string(18) "This is an example"'

我只想在开始时获得没有这些东西的字符串,所以:

'This is an example'

但是回报(以及括号中的数字)可能会有所不同。 我想在每种情况下都去掉这个。

您可以使用以下正则表达式来实现这一点:

/.+\)\s\"(.*)\"/

如下:

  • .+ - 一个或任意数量的字符
  • \\)\\s\\" - 直到你到达) "
  • (.*) - 然后捕获所有内容
  • \\" - 直到你到达最后的"

工作示例:

 const processString = (string) => { let processedString = string.replace(/.+\\)\\s\\"(.*)\\"/, '$1'); console.log(processedString); } processString('string(18) "This is an example"'); processString('string(23) "This is another example"'); processString('string(25) "This is a "third" example"');

string.replace(/^.*? "|"$/g, '')

证明

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
   "                       ' "'
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

JavaScript 代码:

 const string = 'string(18) "This is an example"'; console.log(string.replace(/^.*? "|"$/g, ''));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM