[英]Remove all characters on line after a specific character in multiline textarea
Say I have a multi line text area with one URL per line, and I want to remove all the queries and tags after the actual link on each line, how can I accomplish this? 假设我有一个多行文本区域,每行有一个URL,我想删除每行上实际链接后的所有查询和标记,我该如何实现? I was thinking of just looking for a "?"
我在想只是在寻找一个“?” and just erasing the "?"
并且只是删除了“?” along with all the content after it.
以及之后的所有内容。 I need to loop through all the lines and output the URLS without the queries.
我需要循环遍历所有行并输出URL而不进行查询。
I found this script that can remove the characters after the ? 我发现这个脚本可以删除后的字符? but i am not sure how to make it work with a multiline text box.
但我不知道如何使用多行文本框。
ORIGINAL 原版的
<textarea>
https://www.amazon.com/gp/product/B076736W8H?pf_rd_p=c2945051-950f-485c-b4df-15aac5223b10&pf_rd_r=9WBS7FV4YHRG3SFC42GK
https://www.amazon.com/dp/B07FGG4SNM/ref=sspa_dk_detail_6?psc=1&pd_rd_i=B07FGG4SNM&pd_rd_w=LD3nX&pf_rd_p=21517efd-b385-405b-a405-9a37af61b5b4&pd_rd_wg=L6K9T&pf_rd_r=RZJNTFFGASGBYNA71403&pd_rd_r=8d24e9dc-1470-11e9-a15a-092e7e681a0e
<textarea/>
DESIRED OUTPUT 期望的输出
<textarea>
https://www.amazon.com/gp/product/B076736W8H
https://www.amazon.com/dp/B07FGG4SNM/ref=sspa_dk_detail_6
<textarea/>
Script I found: 脚本我发现:
var s = '/Controller/Action';
var n = s.indexOf('?');
s = s.substring(0, n != -1 ? n : s.length);
document.write(s);
Any help is appreciated! 任何帮助表示赞赏! This is my first little project in Html/JS.
这是我在Html / JS中的第一个小项目。
You can use a global regular expression to match lines which start with http
and contain a ?
您可以使用全局正则表达式来匹配以
http
开头并包含?
, and replace the whole line with only the part before the ?
,并且只用前面的部分替换整条线
?
: :
const textarea = document.querySelector('textarea'); textarea.value = textarea.value.replace(/^(http[^?]+)\\?.+$/gm, '$1');
textarea { width: 100%; }
<textarea> https://www.amazon.com/gp/product/B076736W8H?pf_rd_p=c2945051-950f-485c-b4df-15aac5223b10&pf_rd_r=9WBS7FV4YHRG3SFC42GK https://www.amazon.com/dp/B07FGG4SNM/ref=sspa_dk_detail_6?psc=1&pd_rd_i=B07FGG4SNM&pd_rd_w=LD3nX&pf_rd_p=21517efd-b385-405b-a405-9a37af61b5b4&pd_rd_wg=L6K9T&pf_rd_r=RZJNTFFGASGBYNA71403&pd_rd_r=8d24e9dc-1470-11e9-a15a-092e7e681a0e </textarea>
The pattern 模式
^(http[^?]+)\?.+$
means: 手段:
^
- Match the start of the string ^
- 匹配字符串的开头 (http[^?]+)
- Match and capture a group containing (http[^?]+)
- 匹配并捕获包含的组
http[^?]+
- http
, followed by non- ?
http[^?]+
- http
,其次是非?
characters \\?
- Match a literal ?
?
.+$
- Match the rest of the characters in the line .+$
- 匹配行中的其余字符 And replace with $1
, the first captured group. 并以
$1
替换第一个被捕获的组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.