Detecting two consecutive carriage returns at the end of the string
Is there a way to detect if there are 2 consecutive carriage returns in a string ontained from a textarea or multiline text box?
Here is the scenario: In a text area, user enters ABCD "Enter" EFGHI "Enter" JKLMNOP "Enter" "Enter". After the this I need to force click event of the button - not form.submit.
Here is the default.aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/UserControls/Search.ascx" TagName="Search" TagPrefix="ucSearch" %>
<html xmlns="w3.org/1999/xhtml">;
<head runat="server">
<title></title> </head>
<body>
<form id="form1" runat="server">
<asp:scriptmanager runa开发者_开发百科t="server"></asp:scriptmanager>
<div>
<ucSearch:Search id="search1" runat="server" />
</div>
</form>
</body>
</html>
This is the Search.ascx page:
<script language="javascript">
var inputString function doit(){inputString = document.getElementById("search1$txtSearchText").value;
if (inputString.match(/(\n\n|\r\r|\r\n\r\n)$/)) {
document.getElementById("search1_btnFindAssets").click();
}
</script>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Search.ascx.cs" Inherits="UserControls_Search"%>
<asp:TextBox ID="txtSearchText" TextMode="MultiLine"onKeyPress="doit();" runat="server">
</asp:TextBox>
<br>
<asp:ButtonID="btnFindAssets"runat="server"Text="Find"onclick="btnFindAssets_Click">
var isDoubled = yourString.indexOf("\n\n") != -1;
Yes, you can do this with a regular expression:
if (s.match(/\r\r/)) { ... }
The \r
character matches carriage return. Maybe you mean line feed (\n
)? You might also want to handle different type of new line '\r', '\r\n', or '\n'. You can do this like this:
if (s.match(/\n\n|\r\r|\r\n\r\n/)) { ... }
If you only want the match at the end of the string, use the regex symbol $
:
if (s.match(/\r\r$/)) { ... }
or:
if (s.match(/(\n\n|\r\r|\r\n\r\n)$/)) { ... }
if (/[\r\n]{2,}/.test(myString))
{
//TODO
}
Looks for two or more consecutive carrage return/new lines any where in a string.
精彩评论