1

I am trying to replace multiple string values at once from one variable and pass it into a new one from form data.

My final outcome is to make a very simple (and crude) encrypt\decrypt program. This is what I have so far. I can't seem to find anything on where to go from here.

function testResults (form) {
    var TestVar = form.inputbox.value;
    var NewVar = TestVar.replace(/a/g, "b").replace(/b/g, "c");
    alert ("Replaced text: " + NewVar);
}
1
  • What do you want to do? The regex is correct in your code. Commented Feb 5, 2012 at 6:11

4 Answers 4

1

I do not think a regex is the right way to go for an obfuscation algorithm. You should manipulate the strings with your own code algorithm, not with a regex. Here's a simple way to bump up every character in the string by one value like you were trying to do with the regex:

function testResults (form) {
    var input = form.inputbox.value;
    var output = [];
    for (var i = 0; i < input.length; i++) {
        output[i] = String.fromCharCode(input.charCodeAt(i) + 1);
    }
    output = output.join("");
    alert ("Replaced text: " + output);
}

You can see an example here: http://jsfiddle.net/jfriend00/LYnAV/.

FYI, please don't call this encryption as it is not encryption. It's obscuration (obscuring the original value in a non-secure fashion).

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand correctly, you want to replace every 'a' with 'b', every 'b' with 'c', etc., but if you do this directly (as in your example), you get all 'b' generated from the 'a' finally replaced with 'c', which is not what you want.

Maybe you can work in 2 stages:

A. replace each char with a special string,

B. replace each special string with the encryption.

For example, your example will look like this:

var TempVar = TestVar.replace(/a/g, "{{{a}}}").replace(/b/g, "{{{b}}}");
var NewVar  = TempVar.replace(/{{{a}}}/g, "b").replace(/{{{b}}}/g, "c");

Comments

1

If you are trying to shift all ascii codes of the symbols by one then your attempt to do it is not very good. Look at this code http://jsfiddle.net/AHgZq/

$('#in').keyup(function(){
  var val = $(this).val(), newval = '';
    for(var i=0; i<val.length; i++)
    {
        newval += String.fromCharCode(val.charCodeAt(i)+1);
    }
  $('#out').val(newval);  
});

On any letter entered in the input with id='in' it takes the whole string, takes every letter in it. Gets the ASCII code of it with function charCodeAt, increases it by one and converts back to ASCII symbol with the help of fromCharCode function. After that it sets the value of input with id='out' to that shifted string. Sample code uses jQuery for the fast access to the elements, but does not require it in general.

Or you can do it with regexp http://jsfiddle.net/8sMvg/1/

$('#in').keyup(function(){
  var val = $(this).val(), newval = ''; 
  newval = val.replace(/(\w)/g,
          function(match,group){ 
                       return String.fromCharCode(match.charCodeAt(0)+1);
           }); 
  $('#out').val(newval);  
});

With respect to your code it will look like

function testResults (form) {
    var TestVar = form.inputbox.value;
    var NewVar = TestVar.replace(/(\w)/g,
              function(match){ 
                  return String.fromCharCode(match.charCodeAt(0)+1);
               }); 
    alert ("Replaced text: " + NewVar);
}

Comments

1

If you want to shift letters, you shouldn't use replace this way.

I suggest you do it this way:

function shift(str, num) {
    var res = "";

    for (var i = 0; i < str.length; i++) {
        str = str.toUpperCase();
        var ch = str.charAt(i);
        if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(ch) != -1) {
            var code = str.charCodeAt(i);
            var shiftedCode = 'A'.charCodeAt() + (code - 'A'.charCodeAt() + num) % ('Z'.charCodeAt() - 'A'.charCodeAt() + 1);
            res += String.fromCharCode(shiftedCode);
        } else {
            res += ch;
        }
    }

    return res;    
}

function encStr(str) {
    return shift(str, 1);
}

function decStr(str) {
    return shift(str, 25);
}

alert(decStr(encStr("this string is encrypted and decrypted correctly!")));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.