我无法显示文本区域中的链接数。我使用以下代码:
Assembly assembly = Assembly.GetExecutingAssembly();
string resourceName = "FirstNames.csv";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
CsvReader csvReader = new CsvReader(reader);
csvReader.Configuration.MissingFieldFound = null;
csvReader.Configuration.HeaderValidated = null;
var firstNames = csvReader.GetRecords<FirstName>().ToArray();
context.FirstNames.AddOrUpdate(f => f.Name, firstNames);
}
}
但这显示了网址的名称,例如:
插入文本区域:
<script>
$(function() {
"use strict";
var url = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
$("#textarea").on("keyup", function( e ) {
var urls, output = "";
if ( e.keyCode !== 8 && e.keyCode !== 9 && e.keyCode !== 13 && e.keyCode !== 32 && e.keyCode !== 46 ) {
// Return is backspace, tab, enter, space or delete was not pressed.
return;
}
while ((urls = url.exec(this.value)) !== null) {
output += urls[0] + ", ";
}
$("#result").html("URLS: " + output.substring(0, output.length - 2));
});
});
</script>
<textarea id="textarea" style="height: 300px; width: 500px;"></textarea>
<div id="result"></div>
显示:
https://www.google.com
https://www.amazon.com
但是我需要显示:
URLS: https://www.google.com, https://www.amazon.com
我尝试:
URLS: 2
但是获取URL的字母数。
有人可以帮助我吗?非常感谢
答案 0 :(得分:0)
如果将output
设置为数组而不是字符串,则可以使用length
属性:
var output = [];
和
while ((urls = url.exec(this.value)) !== null) {
output.push( urls[0] );
}
和
$("#result").html("URLS: " + output.length );
"use strict";
var url = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
document.querySelector( 'textarea' ).addEventListener( 'keyup', function( e ) {
var urls;
var output = [];
if ( e.keyCode !== 8 && e.keyCode !== 9 && e.keyCode !== 13 && e.keyCode !== 32 && e.keyCode !== 46 ) {
// Return is backspace, tab, enter, space or delete was not pressed.
return;
}
while ((urls = url.exec(this.value)) !== null) {
output.push( urls[ 0 ] );
}
document.querySelector( '#result' ).innerHTML = "URLS: " + output.length;
});
textarea {
height: 100px;
width: 400px;
}
<textarea></textarea>
<div id="result"></div>
答案 1 :(得分:0)
可以使用变量i进行此操作,并在循环外将其初始化为零。并为每次循环迭代将i增加1。
答案 2 :(得分:0)
从以下位置更改代码:
$(function() {
"use strict";
var url = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
$("#textarea").on("keyup", function( e ) {
var urls, output = "";
if ( e.keyCode !== 8 && e.keyCode !== 9 && e.keyCode !== 13 && e.keyCode !== 32 && e.keyCode !== 46 ) {
// Return is backspace, tab, enter, space or delete was not pressed.
return;
}
while ((urls = url.exec(this.value)) !== null) {
output += urls[0] + ", ";
}
$("#result").html("URLS: " + output.substring(0, output.length - 2));
});
});
收件人:
$(function() {
"use strict";
var url = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gmi;
$("#textarea").on("keyup", function( e ) {
var urls, output = "";
if ( e.keyCode !== 8 && e.keyCode !== 9 && e.keyCode !== 13 && e.keyCode !== 32 && e.keyCode !== 46 ) {
// Return is backspace, tab, enter, space or delete was not pressed.
return;
}
var urls = this.value.match(url);
$("#result").html(urls.length);
});
});
https://codepen.io/jthomas077/pen/gjZbPe
注意:如果需要,您仍然可以遍历比赛。