Link to home
Start Free TrialLog in
Avatar of Blessed7777
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><br />
       <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"></script>



<script>

    $('#one').keyup(function (e) {
        var txtVal = $(this).val();
        $('#two').val(txtVal.length);
        $('#three').val(txtVal.match(/\b(\w){1,2}/g));
        $('#four').val(txtVal.match(/\b(\w)|(\w)\b/g));
       
    });
</script>
    </div>
    </form>
</body>

I appreciate your help with this.  Please let me know if you need more details.
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Is this homework?

Anyway, this is not a regex problem, it's a substring problem.
Avatar of Blessed7777
Blessed7777

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?
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);
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your help with his hielo.  I am new to regex so I have a little bit of a learning curve.