Reputation: 719
On a create topic page I have a textarea which is filled in partially by code. Everything between the confidential
tags is added by this code.
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.
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}
.
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:
This should return:
example.com
Upvotes: 1
Views: 53
Reputation: 177885
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>
Upvotes: 2
Reputation: 964
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:
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:
Split the string in an array with a regex. this way you will always get a odd length array.
Select the middle position of the array with Math.round and division.
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.
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>
Upvotes: 2