[英]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, '')
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.