Blessed7777
asked on
Help with a Javascript/Regex issue
Hello, I am trying to take a string and manipulate some of the characters using Regex Ecmascript version. Here is the scenario:
String is called "Hello World gr8tly".
Steps:
1. Capture the first and last characters of each string. Result: Ho, Wd, gy
2. Capture the remaining characters from each. result: ell, orl, r8tl
3. Capture distinct. result: el, orl, r8tl
4. Count distinct. result: 2, 3, 4
5. Join steps 1 and 4 together. result: H2o, W3d, g48tl. White space should not be counted. Need to allow for special characters
Here is my current code:
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Enter word or phrase in this box:"></asp:Label><br />
<textarea id="one" runat= "server" style="overflow:hidden; width:500px; height:100px"></textarea>< br />
<asp:Label ID="Label2" runat="server" Text="Here Is detailed string breakdown:"></asp:Label><b r />
<textarea id="two" runat="server" style="overflow:hidden; width:500px; height:100px"></textarea>< br />
<asp:Label ID="Label3" runat="server" Text="This shows the speacial 3 character code: "></asp:Label><br />
<textarea id="three" runat="server" style="overflow:hidden; width:500px; height:100px"></textarea>< br />
<asp:Label ID="Label4" runat="server" Text="This shows the speacial 3 character code: "></asp:Label><br />
<textarea id="four" runat="server" style="overflow:hidden; width:500px; height:100px"></textarea>< br />
<input id="Reset1" type="reset" value="reset" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></s cript>
<script>
$('#one').keyup(function (e) {
var txtVal = $(this).val();
$('#two').val(txtVal.lengt h);
$('#three').val(txtVal.mat ch(/\b(\w) {1,2}/g));
$('#four').val(txtVal.matc h(/\b(\w)| (\w)\b/g)) ;
});
</script>
</div>
</form>
</body>
I appreciate your help with this. Please let me know if you need more details.
String is called "Hello World gr8tly".
Steps:
1. Capture the first and last characters of each string. Result: Ho, Wd, gy
2. Capture the remaining characters from each. result: ell, orl, r8tl
3. Capture distinct. result: el, orl, r8tl
4. Count distinct. result: 2, 3, 4
5. Join steps 1 and 4 together. result: H2o, W3d, g48tl. White space should not be counted. Need to allow for special characters
Here is my current code:
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Enter word or phrase in this box:"></asp:Label><br />
<textarea id="one" runat= "server" style="overflow:hidden; width:500px; height:100px"></textarea><
<asp:Label ID="Label2" runat="server" Text="Here Is detailed string breakdown:"></asp:Label><b
<textarea id="two" runat="server" style="overflow:hidden; width:500px; height:100px"></textarea><
<asp:Label ID="Label3" runat="server" Text="This shows the speacial 3 character code: "></asp:Label><br />
<textarea id="three" runat="server" style="overflow:hidden; width:500px; height:100px"></textarea><
<asp:Label ID="Label4" runat="server" Text="This shows the speacial 3 character code: "></asp:Label><br />
<textarea id="four" runat="server" style="overflow:hidden; width:500px; height:100px"></textarea><
<input id="Reset1" type="reset" value="reset" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></s
<script>
$('#one').keyup(function (e) {
var txtVal = $(this).val();
$('#two').val(txtVal.lengt
$('#three').val(txtVal.mat
$('#four').val(txtVal.matc
});
</script>
</div>
</form>
</body>
I appreciate your help with this. Please let me know if you need more details.
ASKER
What is the solution? No this is not homework, I am trying to learn regex on my own time. I this something you can assist with?
ASKER
I tried concatenating the results from each line. The individual lines are working correctly showing str1=H,W,g
str2=o,d,y
in stead of Ho,Wd,gy I get H,W,g,o,d,y. How does var newString = str1.concat(str2); need to be in order to get the correct order?
Here is my code:
$('#one').keyup(function (e) {
var txtVal = $(this).val();
var str1 = (txtVal.match(/\b(\w)/g));
var str2 = (txtVal.match(/(\w)\b/g));
$('#three').val(str1);
$('#four').val(str2);
var newString = str1.concat(str2);
document.write(newString);
str2=o,d,y
in stead of Ho,Wd,gy I get H,W,g,o,d,y. How does var newString = str1.concat(str2); need to be in order to get the correct order?
Here is my code:
$('#one').keyup(function (e) {
var txtVal = $(this).val();
var str1 = (txtVal.match(/\b(\w)/g));
var str2 = (txtVal.match(/(\w)\b/g));
$('#three').val(str1);
$('#four').val(str2);
var newString = str1.concat(str2);
document.write(newString);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your help with his hielo. I am new to regex so I have a little bit of a learning curve.
Anyway, this is not a regex problem, it's a substring problem.