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

时间:2019-06-21 15:58:56

标签: javascript

在创建主题页面上,我有一个文本区域,该区域部分由代码填充。 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]

此代码在同一页面上运行:

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

这基本上用新的站点详细信息替换了站点详细信息。 inputValues在此处不可见,但其中包含站点详细信息并且正在运行。

通过更改以下行,也可以轻松地修改以上代码以删除网站详细信息:

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

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

但是我如何修改代码以保留{sitedetails}{/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!

如果可能的话,我该如何走得更远,只保持在Site URL:Site Username:之间

这应该返回:

example.com

2 个答案:

答案 0 :(得分:2)

好吧,据我所知,我可能错了,您想要提取给定字符串上Site URL:和Site Username:之间的所有内容,并将其存储到变量中,然后可以从这样输入字符串:

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

这样您就可以获取它。在那里做的是:

  1. 使用正则表达式将字符串拆分为数组。这样,您将始终获得奇数长度的数组。

  2. 使用Math.round和除法选择数组的中间位置。

  3. 修剪以删除空格。

仅当字符串中只有一个“站点URL:”和“站点用户名:”匹配时,此逻辑才起作用,但是可以在其他情况下进行转换。

摘要:

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>

答案 1 :(得分:2)

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>

相关问题