admin管理员组

文章数量:1323688

I'm trying to change a value in textarea to such that it can contain only hex digits.
For example, if I have this in the textarea:

    RMCP01
    Mario Kart Wii

    CCP items
    * C27EF0EC 00000002
    * 3FE08000 93BF1500
    * 3BE00000 00000000
    * 28345842 ff7f0080
    * 8000000F 00000000
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fffb0004
    * 8000000F 00000020
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fffd0002
    * 8000000F 0000000B
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 7fff8000
    * 8000000F 00000009
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 bfff4000
    * 8000000F 00000004
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 efff1000
    * 8000000F 0000000E
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fbff0400
    * 8000000F 0000000F
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fff70008
    * 8000000F 00000007
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 ffdf0020
    * 8000000F 00000003
    * 8000000E 00000001
    * E0000000 00000000
    * E2000001 00000000
    * 48000000 809C1900
    * DE000000 80008180
    * 58010000 00000008
    * DE000000 80008180
    * 48100000 80001500
    * DE000000 80008180
    * 9421000F 0000008c
    * 14000090 00000001
    * E0000000 80008000

    Connect Hacker
    * 046592D8 3BA0270F

    VR
    * 28345842 ff7b0084
    * 48000000 809BD748
    * DE000000 90009380
    * 5A010000 00009050
    * 12000000 00008ae3
    * E0000000 00000000
    * 28345842 ddff2200
    * 48000000 809BD748
    * DE000000 90009380
    * 5A010000 00009050
    * 12000000 00000001
    * E0000000 00000000
    * 28345842 ff870078
    * 48000000 809BD748
    * DE000000 90009380
    * 5A010000 00009050
    * 12000000 00001388
    * CC000000 00000000
    * E0000000 80008000

    RBC
    * 045334B0 38000002
    * 04533510 2C000003

    Drift
    * 048B5CC0 00000000
    * 048B5CC4 00000000
    * 048B5CC8 00010000
    * 048B5B38 00010000

    Invicibilty
    * F6000001 80008100
    * A88301A8 3BC00000
    * 1400002C 60000000
    * F6000001 80008100
    * EC4300B2 C0630088
    * D2000020 00000002
    * 38000001 9803000A
    * 80030008 00000000
    * E0000000 80008000

    BBB
    283457E2 BFFE4001
    048B59F8 43960000
    E0000000 00000000
    283457E2 BFFD4002
    048B59F8 442F0000
    E0000000 00000000
    283457E2 FBEF0410
    048B59F8 43110000
    E0000000 00000000
    283457E2 EBFF1400
    048B59F8 40000000
    E0000000 00000000

    Deactivator
    * 28348200 FEFF0100
    * 8000000D 00000001
    * 48000000 809C1900
    * DE000000 80008180
    * 58010000 00000008
    * DE000000 80008180
    * 4A100000 0000008C
    * 30000000 00000014
    * 14000000 00000014
    * 14000004 00000000
    * E0000000 00000000
    * 28348200 FDFF0200
    * 8000000D 00000000
    * E0000000 80008000

The result is everything but the first character. (The first line changes to "MCP01") This is my code:

        function check() {
            var c=$("#c").value;
            c=c.replace(/[^A-Fa-f0-9]/, "");
            return c;
        }

What is wrong?

I'm trying to change a value in textarea to such that it can contain only hex digits.
For example, if I have this in the textarea:

    RMCP01
    Mario Kart Wii

    CCP items
    * C27EF0EC 00000002
    * 3FE08000 93BF1500
    * 3BE00000 00000000
    * 28345842 ff7f0080
    * 8000000F 00000000
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fffb0004
    * 8000000F 00000020
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fffd0002
    * 8000000F 0000000B
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 7fff8000
    * 8000000F 00000009
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 bfff4000
    * 8000000F 00000004
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 efff1000
    * 8000000F 0000000E
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fbff0400
    * 8000000F 0000000F
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 fff70008
    * 8000000F 00000007
    * 8000000E 00000001
    * E0000000 00000000
    * 28345842 ffdf0020
    * 8000000F 00000003
    * 8000000E 00000001
    * E0000000 00000000
    * E2000001 00000000
    * 48000000 809C1900
    * DE000000 80008180
    * 58010000 00000008
    * DE000000 80008180
    * 48100000 80001500
    * DE000000 80008180
    * 9421000F 0000008c
    * 14000090 00000001
    * E0000000 80008000

    Connect Hacker
    * 046592D8 3BA0270F

    VR
    * 28345842 ff7b0084
    * 48000000 809BD748
    * DE000000 90009380
    * 5A010000 00009050
    * 12000000 00008ae3
    * E0000000 00000000
    * 28345842 ddff2200
    * 48000000 809BD748
    * DE000000 90009380
    * 5A010000 00009050
    * 12000000 00000001
    * E0000000 00000000
    * 28345842 ff870078
    * 48000000 809BD748
    * DE000000 90009380
    * 5A010000 00009050
    * 12000000 00001388
    * CC000000 00000000
    * E0000000 80008000

    RBC
    * 045334B0 38000002
    * 04533510 2C000003

    Drift
    * 048B5CC0 00000000
    * 048B5CC4 00000000
    * 048B5CC8 00010000
    * 048B5B38 00010000

    Invicibilty
    * F6000001 80008100
    * A88301A8 3BC00000
    * 1400002C 60000000
    * F6000001 80008100
    * EC4300B2 C0630088
    * D2000020 00000002
    * 38000001 9803000A
    * 80030008 00000000
    * E0000000 80008000

    BBB
    283457E2 BFFE4001
    048B59F8 43960000
    E0000000 00000000
    283457E2 BFFD4002
    048B59F8 442F0000
    E0000000 00000000
    283457E2 FBEF0410
    048B59F8 43110000
    E0000000 00000000
    283457E2 EBFF1400
    048B59F8 40000000
    E0000000 00000000

    Deactivator
    * 28348200 FEFF0100
    * 8000000D 00000001
    * 48000000 809C1900
    * DE000000 80008180
    * 58010000 00000008
    * DE000000 80008180
    * 4A100000 0000008C
    * 30000000 00000014
    * 14000000 00000014
    * 14000004 00000000
    * E0000000 00000000
    * 28348200 FDFF0200
    * 8000000D 00000000
    * E0000000 80008000

The result is everything but the first character. (The first line changes to "MCP01") This is my code:

        function check() {
            var c=$("#c").value;
            c=c.replace(/[^A-Fa-f0-9]/, "");
            return c;
        }

What is wrong?

Share asked Dec 23, 2012 at 11:17 gskartwiigskartwii 3893 gold badges7 silver badges18 bronze badges 1
  • 1 it should be $("#c").val(), and this logic won't work because Deactivator has bunch of valid hex characters in it – Esailija Commented Dec 23, 2012 at 11:30
Add a ment  | 

2 Answers 2

Reset to default 8

You should use /g flag to perform global replacement: -

c=c.replace(/[^A-Fa-f0-9]/g, "");

Note that, this approach may fail, because in "ABRET", it will only replace R and T, but ABE is not a valid hex digit.

If you want to keep just hex digit, then you should probably change your regex to: -

/\b[0-9a-fA-F]{6}\b/g

The above regex will only match Hex digit. So, just replace every substring, that doesn't matches the above pattern. Rest I leave to you to implement.

You can also modify the above regex to: -

/\b[0-9A-F]{6}\b/gi

With /i flag, the regex does case-insensitive matches. So, A is same as a.

These codes did not work enough good but they are great. Here is the code I ended up using:

var c=$("c").val();
var newc="";
var counter=0;
var newcstack="";
for (var i=0; i<c.length; i++) {
    if (c.charAt(i).match(/[A-Fa-f0-9]/)) {
        newcstack+=c.charAt(i);
        counter++;
        if (counter==8) {
            newc+=newcstack;
            counter=0;
            newcstack="";
        }
    }
    else {
        counter=0;
        newcstack="";
    }
}
return newc;

本文标签: regexJavaScriptRemove Everything from String But HexStack Overflow