简体   繁体   English

从多行文本区域中提取两个单词之间

[英]Extracting between two words from Multiline textarea

On a create topic page I have a textarea which is filled in partially by code. 在创建主题页面上,我有一个textarea,部分用代码填充。 Everything between the confidential tags is added by this code. 此代码添加了confidential标签之间的所有内容。

There is some text in the message!


[confidential]
{sitedetails}

Site URL: example.com
Site Username: test
Site Password: test

FTP URL: ftp.domain.com
FTP Username: test
FTP Password: test

Optional Information: Just some text for testing purposes!

{/sitedetails}
[/confidential]

On this same page this code runs: 此代码在同一页面上运行:

var $editor = $(".markItUpEditor");
var curValue = $editor.val();
var sdCheck = curValue;
var sdAnalyze = /{sitedetails}([\S\s]*?){\/sitedetails}/gm
var newSD = sdCheck.replace(sdAnalyze,"{sitedetails}\n\n" + inputValues + "\n\n{/sitedetails}");
//alert(newSD);
$editor.val(newSD);

This basically replaces the site details with new site details. 这基本上用新的站点详细信息替换了站点详细信息。 the inputValues are not visible here but they contain the site details and are working. inputValues在此处不可见,但它们包含站点详细信息并且正在运行。

The above code could also easily be modified to remove the site details by changing this line: 通过更改此行,也可以轻松地修改以上代码以删除站点详细信息:

var newSD = sdCheck.replace(sdAnalyze,"{sitedetails}\n\n" + inputValues + "\n\n{/sitedetails}");

to

var newSD = sdCheck.replace(sdAnalyze,"");

But how can I modify the code to keep everything that is between {sitedetails} and {/sitedetails} . 但是,如何修改代码以保留{sitedetails}{/sitedetails}之间的所有内容。

This should return: 这应该返回:

Site URL: example.com
Site Username: test
Site Password: test

FTP URL: ftp.domain.com
FTP Username: test
FTP Password: test

Optional Information: Just some text for testing purposes!

And if possible how can I go even further and keep only between Site URL: and Site Username: 而且,如果可能的话,我该如何走得更远,只保留在Site URL:Site Username:

This should return: 这应该返回:

example.com

Well, for what I understand, and I may be wrong, you want to extract everything that is between Site URL: and Site Username: on the given string, and perhaps store it into a variable, then you can extract from a string this way: 好了,据我所知,我可能是错的,您想提取给定字符串上Site URL:和Site Username:之间的所有内容,然后将其存储到变量中,然后可以通过这种方式从字符串中提取内容:

sdCheck.split(/(Site URL:|Site Username:)/g)[Math.round((sdCheck.split(/(Site URL:|Site Username:)/g).length - 1) / 2)].trim();

This way you can get it. 这样您就可以得到它。 And what was done there is: 在那里做的是:

  1. Split the string in an array with a regex. 使用正则表达式将字符串拆分为数组。 this way you will always get a odd length array. 这样,您将始终获得奇数长度的数组。

  2. Select the middle position of the array with Math.round and division. 用Math.round和divit选择数组的中间位置。

  3. A trim to remove whitespace. 修剪以删除空格。

This logic will only work if there is only one Site URL: and Site Username: match in the string, but it can be transformed for other scenarios. 仅当字符串中只有一个“站点URL:”和“站点用户名:”匹配时,此逻辑才有效,但是可以针对其他情况进行转换。

Snippet: 片段:

 var textarea = document.getElementById('textarea'); extract(); function extract() { var text = textarea.value; document.getElementById('result').innerHTML = text.split(/(Site URL:|Site Username:)/g)[Math.round((text.split(/(Site URL:|Site Username:)/g).length - 1) / 2)].trim(); } textarea.addEventListener('keyup', function() { extract(); }); 
 <textarea id="textarea" style="height: 200px; display: inline-block"> There is some text in the message! [confidential] {sitedetails} Site URL: example.com Site Username: test Site Password: test FTP URL: ftp.domain.com FTP Username: test FTP Password: test Optional Information: Just some text for testing purposes! {/sitedetails} [/confidential] </textarea> <div id="result" style="display: inline-block; vertical-align: top"> </div> 

Like this 像这样

 var re = /{sitedetails}([\\S\\s]*?){\\/sitedetails}/gm, urlRe = /Site URL:(.*)\\n/ var str = $(".editor").val(), newStr = re.exec(str); newStr = newStr ? newStr[1].trim() : ""; console.log(newStr) if (newStr) { var url = newStr.match(urlRe); if (url) url = url[1].trim(); console.log("URL:",url) } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class="editor" rows="10">There is some text in the message! [confidential] {sitedetails} Site URL: example.com Site Username: test Site Password: test FTP URL: ftp.domain.com FTP Username: test FTP Password: test Optional Information: Just some text for testing purposes! {/sitedetails} [/confidential]</textarea> 

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

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