如何创建子串,替换子串?

时间:2015-08-04 00:58:15

标签: javascript regex pdf

使用以下代码我转换一个字符串:

1Z3245799938735257

成:

1Z›324579š99›38735257

但是现在,我想将“”和“š”之间以及“”和该字符串末尾之间的区域分成对。

因此子结果是:

1Z›32,45,79š99›38,73,52,57

现在我想将对更改为String.fromCharCode(pair+32)

因此子结果是:

1Z›@,M,oš99›F,i,T,Y

","替换"" 所以最终的结果是

1Z›@Moš99›FiTY

我该怎么做?

var chars = new Array();
chars["00"] = " "; chars["01"] = "!"; chars["02"] = "“"; chars["03"] = "#"; chars["04"] = "$"; 
chars["05"] = "%"; chars["06"] = "&"; chars["07"] = "’"; chars["08"] = "("; chars["09"] = ")";
chars["10"] = "*"; chars["11"] = "+"; chars["12"] = ","; chars["13"] = "-"; chars["14"] = ".";
chars["15"] = "/"; chars["16"] = "0"; chars["17"] = "1"; chars["18"] = "2"; chars["19"] = "3";
chars["20"] = "4"; chars["21"] = "5"; chars["22"] = "6"; chars["23"] = "7"; chars["24"] = "8";
chars["25"] = "9"; chars["26"] = ":"; chars["27"] = ";"; chars["28"] = "<"; chars["29"] = "=";
chars["30"] = ">"; chars["31"] = "?"; chars["32"] = "@"; chars["33"] = "A"; chars["34"] = "B";
chars["35"] = "C"; chars["36"] = "D"; chars["37"] = "E"; chars["38"] = "F"; chars["39"] = "G";
chars["40"] = "H"; chars["41"] = "I"; chars["42"] = "J"; chars["43"] = "K"; chars["44"] = "L";
chars["45"] = "M"; chars["46"] = "N"; chars["47"] = "O"; chars["48"] = "P"; chars["49"] = "Q";
chars["50"] = "R"; chars["51"] = "S"; chars["52"] = "T"; chars["53"] = "U"; chars["54"] = "V";
chars["55"] = "W"; chars["56"] = "X"; chars["57"] = "Y"; chars["58"] = "Z"; chars["59"] = "[";
chars["60"] = "\\"; chars["61"] = "]"; chars["62"] = "^"; chars["63"] = "_"; chars["64"] = "`";
chars["65"] = "a"; chars["66"] = "b"; chars["67"] = "c"; chars["68"] = "d"; chars["69"] = "e";
chars["70"] = "f"; chars["71"] = "g"; chars["72"] = "h"; chars["73"] = "i"; chars["74"] = "j";
chars["75"] = "k"; chars["76"] = "l"; chars["77"] = "m"; chars["78"] = "n"; chars["79"] = "o"; 
chars["80"] = "p"; chars["81"] = "q"; chars["82"] = "r"; chars["83"] = "s"; chars["84"] = "t";
chars["85"] = "u"; chars["86"] = "v"; chars["87"] = "w"; chars["88"] = "x"; chars["89"] = "y";
chars["90"] = "z"; chars["91"] = "{"; chars["92"] = "|"; chars["93"] = "}"; chars["94"] = "~";

var charsv = new Array();
charsv[" "] = 128; charsv["!"] = 01; charsv['"'] = 02; charsv["#"] = 03; charsv["$"] = 04; 
charsv["%"] = 05; charsv["&"] = 06; charsv["’"] = 07; charsv["("] = 08; charsv[")"] = 09; 
charsv["*"] = 10; charsv["+"] = 11; charsv[","] = 12; charsv["-"] = 13; charsv["."] = 14; 
charsv["/"] = 15; charsv["0"] = 16; charsv["1"] = 17; charsv["2"] = 18; charsv["3"] = 19;
charsv["4"] = 20; charsv["5"] = 21; charsv["6"] = 22; charsv["7"] = 23; charsv["8"] = 24; 
charsv["9"] = 25; charsv[":"] = 26; charsv[";"] = 27; charsv["<"] = 28; charsv["="] = 29; 
charsv[">"] = 30; charsv["?"] = 31; charsv["@"] = 32; charsv["A"] = 33; charsv["B"] = 34; 
charsv["C"] = 35; charsv["D"] = 36; charsv["E"] = 37; charsv["F"] = 38; charsv["G"] = 39;
charsv["H"] = 40; charsv["I"] = 41; charsv["J"] = 42; charsv["K"] = 43; charsv["L"] = 44;
charsv["M"] = 45; charsv["N"] = 46; charsv["O"] = 47; charsv["P"] = 48; charsv["Q"] = 49; 
charsv["R"] = 50; charsv["S"] = 51; charsv["T"] = 52; charsv["U"] = 53; charsv["V"] = 54; 
charsv["W"] = 55; charsv["X"] = 56; charsv["Y"] = 57; charsv["Z"] = 58; charsv["["] = 59; 
charsv["\\"] = 60; charsv["]"] = 61; charsv["^"] = 62; charsv["_"] = 63; charsv["`"] = 64; 
charsv["a"] = 65; charsv["b"] = 66; charsv["c"] = 67; charsv["d"] = 68; charsv["e"] = 69;
charsv["f"] = 70; charsv["g"] = 71; charsv["h"] = 72; charsv["i"] = 73; charsv["j"] = 74; 
charsv["k"] = 75; charsv["l"] = 76; charsv["m"] = 77; charsv["n"] = 78; charsv["o"] = 79; 
charsv["p"] = 80; charsv["q"] = 81; charsv["r"] = 82; charsv["s"] = 83; charsv["t"] = 84; 
charsv["u"] = 85; charsv["v"] = 86; charsv["w"] = 87; charsv["x"] = 88; charsv["y"] = 89; 
charsv["z"] = 90; charsv["{"] = 91; charsv["|"] = 92; charsv["}"] = 93; charsv["~"] = 94; 
charsv["š"] = 104; charsv["›"] = 105; charsv["œ"] = 106;

var x = this.getField("S1").valueAsString;
var out = this.getField("B1");

var m = 0;
var transformedString = x.replace(/[A-Z0-9]{2}/g, function(a) {
  if (isNaN(a)) {
    if (m > 0)
      a = 'š' + a;
    m = -1;
  } else {
    if (parseInt(a) > 94) {
      if (m != 0)
        a = 'š' + a;
      m = 2;
    } else {
      a = String.fromCharCode(parseInt(a) + 32);
      if (m == 2 || m == -1)
        a = '›' + a;
      m = 1;
    }
  }
  return a;
});

transformedString = transformedString;

out.value = transformedString;
到目前为止,谢谢你。

但现在是最后几步:

首先:从数组“charsv”中获取每个char的值。

第二:char值的总和:(104 * 1)+(char-value1 * 1)+(char-value2 * 2)+(char-value3 * 3)等等。

第三:得到总和的模数。 (char值的总和%103)。

第四:从数组“chars”中获取此值的char。 if“”,替换为“€”。

第五步:在“transformedString”的开头添加“š”,从第四个添加char,在“transformedString”末尾添加“œ”。

4 个答案:

答案 0 :(得分:4)

jsfiddle(改编我对How to split and rebuild a string?的回答)

var m = 0; // 0 = init, -1 = alpha-num, 1 = numeric < 95, 2 = numeric > 94
var transformedString = "1Z3245799938735257".replace(/[A-Z0-9]{2}/g, function(a) {
  if (isNaN(a)) {
    if (m > 0)
      a = 'š' + a;
    m = -1;
  } else {
    if (parseInt(a) > 94) {
      if (m != 0)
        a = 'š' + a;
      m = 2;
    } else {
      a = String.fromCharCode(parseInt(a) + 32); // the only additional line
      if (m == 2 || m == -1)
        a = '>' + a;
      m = 1;
    }
  }
  return a;
});

答案 1 :(得分:2)

function convert(x) {
    return x.replace(/›[^š]*/g, function (y) {
        var s = "›";
        for (var i = 1; i < y.length; i += 2) {
            var code = parseInt(y.substring(i, i + 2));
            s += String.fromCharCode(code + 32);
        }
        return s;
    });
}

var result = convert("1Z›324579š99›38735257"); // 1Z›@Moš99›FiTY

答案 2 :(得分:1)

这是我的尝试,可能会有点多。

function transform(str) {
  return str.replace(/›[^š]+/g, function(m) {
    m = m.slice(1).replace(/../g, function(c) {
      return String.fromCharCode(parseInt(c) + 32)
    })
    return '›' + m
  });
}

var result = transform('1Z›324579š99›38735257') // 1Z›@Moš99›FiTY

答案 3 :(得分:0)

我认为你可以使用这样的正则表达式:

/(\d{2})(?=\d+)/g

使用替换$1,

[Regex Demo]