Don P
Don P

Reputation: 63678

Find and alter text in a string based on ln # and character #

How can I prepend characters to a word located at a particular Ln # and character # in text?

Example:

The use case is when a person enters code into a textarea (like jsFiddle), I find and replace some of their variables. I know the line # and character location of the start and end of these variables.

Example Text:

var usersCode = $('textarea').val();

console logging usersCode:

print "My first name is: " + first_name
print "This is awesome."
print "My last name is: " + last_name

How could I find the word starting at Ln 0, Char 29 and ending at Ln 0, Char 39 (first_name) and turn it into MyObj.first_name.value?

print "My first name is: " + MyObj.first_name.value
print "This is awesome."
print "My last name is: " + last_name

Maybe I can use a regex that translates "line number" into counting the number of \n occurrences? And then moving the pointer in by the number of characters?

I have to use Ln # and Char # for many details that I won't go into here. I am aware of many simpler alternatives if I wasn't constrained to using Ln # and Ch #.

Upvotes: 0

Views: 186

Answers (3)

Harpreet Singh
Harpreet Singh

Reputation: 2671

RegEx can easily search based on characters but not via position. Though you can still do it using regEx but the soultion will become more and more complex. For this case you don;t need a pure RegEx answer. Code below is what you need.

k=$('#regex_string').val()

function findAndReplaceWord(lineNo, startPos, endPos, replaceWith) {
    var line = k.split(/\n/)[lineNo];
    if(line) {
        var word = line.substring(startPos, endPos)

        return k.replace(word, replaceWith)
    }
}

findAndReplaceWord(0, 29, 39, MyObj.first_name.value)

Upvotes: 0

ameave
ameave

Reputation: 426

You can save the lines of the textarea into an array:

var lines = $('#textarea').val().split(/\n/);

And from there you take the substring of a particular line and assign it to your object:

MyObj.first_name.value = lines[0].substring(29,39)

Hope that helps!

Upvotes: 1

Shelby L Morris
Shelby L Morris

Reputation: 726

If you're just trying to replace first_name and last_name the simplest solution would be to use replace(), for example:

usersCode.replace("first_name", MyObj.first_name.value);
usersCode.replace("last_name", MyObj.last_name.value);

If you're insistent on doing the line number and character number specific thing, we can arrange that, but it's not at all necessary for what it seems like you're asking for. Perhaps I'm mistaken, though.

Update:

Okay so you want to replace all instances? Finding line numbers and all that is still unnecessary, just use this:

usersCode.replace(/first_name/g, MyObj.first_name.value);
usersCode.replace(/last_name/g, MyObj.last_name.value);

g is a RegEx flag for global searching.

Update 2:

usersCode.split("\n")[lineNumber - 1].replace(usersCode.split("\n")[lineNumber - 1].substr(29, 39), MyObj.first_name.value);

You can replace 29 and 39 with variables startChar and endChar respectively. lineNumber will also need to be provided by you.

Upvotes: 0

Related Questions