简体   繁体   English

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

[英]Remove beginning of String with javascript/jquery

I'm getting changing results from a method.我正在从一种方法中获得不断变化的结果。 It is everytime a String, but at the beginning of every String is written "String(xxx) "Text of String... ". The xxx depends on the number of characters inside of the String. I tried using regex to cut everything away before first appearance of " :它每次都是一个字符串,但在每个字符串的开头都写着“String(xxx) “字符串的文本... ”。xxx 取决于字符串内部的字符数。我尝试使用正则表达式来删除所有内容在 " 首次出现之前:

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

But I'm not sure if I used it correctly.但我不确定我是否正确使用它。 I would also accept other approaches, which delete the beginning of the String in every case (independently of the length of the string).我也会接受其他方法,在每种情况下都删除字符串的开头(与字符串的长度无关)。

Example:例子:

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

And I just want to get the String without the stuff at the beginning, so:我只想在开始时获得没有这些东西的字符串,所以:

'This is an example'

But the return (and therefore the number in the parenthesis) can vary.但是回报(以及括号中的数字)可能会有所不同。 I want to cut this out in every case.我想在每种情况下都去掉这个。

You can achieve this with the following regex:您可以使用以下正则表达式来实现这一点:

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

As follows:如下:

  • .+ - one or any number of characters .+ - 一个或任意数量的字符
  • \\)\\s\\" - until you reach ) " \\)\\s\\" - 直到你到达) "
  • (.*) - then capture everything (.*) - 然后捕获所有内容
  • \\" - until you reach the final " \\" - 直到你到达最后的"

Working Example:工作示例:

 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"');

Use

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

See proof .证明

Explanation解释

--------------------------------------------------------------------------------
  ^                        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 code: JavaScript 代码:

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

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

相关问题 使用jQuery或javascript从网页中存在的所有文本框的字符串开头和结尾删除空格 - Remove whitespace from beginning and end of string of all textbox present in a webpage using jQuery or javascript 在Javascript或jQuery中,如何检测字符串开头的空格? - In Javascript or jQuery, how to detect whitespace in beginning of string? 删除 URL 开头的字符串 - Remove the string on the beginning of an URL Javascript从字符串的开头和结尾删除特殊字符 - Javascript remove special characters from beginning and end of string 正则表达式删除所有<br /> javascript中字符串开头和结尾的标签 - Regex to remove all <br /> tags from beginning and end of a string in javascript 去掉 <ul> 字符串开头和结尾的标签(JavaScript或PHP) - Remove <ul> tags from beginning and end of string (JavaScript or PHP) 如何删除 <br> 在使用正则表达式的javascript中的字符串的开头和结尾? - How to remove <br> at the beginning and at the end of a string in javascript using regex? 如何从JavaScript中的字符串开头删除特定模式 - How to remove specific pattern from the beginning of a string in javascript Javascript在开头和结尾删除字符串 - Javascript Remove strings in beginning and end 从 Javascript 中另一个字符串的开头删除一个字符串的最快方法是什么? - What's the fastest way to remove a string from the beginning of another string in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM