I have a function that is provided by the user:
function replace_function(string) { return string.replace(/:smile:/g, '⻇') .replace(/(foo|bar|baz)/g, 'text_$1'); }
and I have an input line as follows:
var input = 'foo bar :smile: xxxx';
and I have a number from 0 to the length of the input string, which I use as a substring to split the string.
I need to find the number (position) that will correspond to the output line after replacement, so that the split is in the same visual place. Separation is just for visualization. I only need a number.
The output string can have the same length, this applies only to the case when the length of the input and output is different (for example, the width function and the input string)
function replace_function(string) { return string.replace(/:smile:/g, '⻇') .replace(/(foo|bar|baz)/g, 'text_$1'); } var textarea = document.querySelector('textarea'); var pre = document.querySelector('pre'); function split() { var input = textarea.value; var output = replace_function(input);
<textarea>xxx foo xxx bar xxx :smile: xxxx</textarea> <pre></pre>
when you click in the middle of the word to be replaced, the position / split should be after the output word. If you click before, between, or after a word, the position must be in the same place (the position in each case will be different from the corresponding place)
UPDATE : here is my code that works for: smile: only input, but you need to have the text before: smile: (input = ": smile: asdas" and the position in the middle of the smile and the position off)
it works for foo replaced by text_foo, but not when there is: smile: before foo (input "asd: smile: asd foo").
var get_position = (function() { function common_string(formatted, normal) { function longer(str) { return found && length(str) > length(found) || !found; } var formatted_len = length(formatted); var normal_len = length(normal); var found; for (var i = normal_len; i > 0; i--) { var test_normal = normal.substring(0, i); var formatted_normal = replace_function(test_normal); for (var j = formatted_len; j > 0; j--) { var test_formatted = formatted.substring(0, j); if (test_formatted === formatted_normal && longer(test_normal)) { found = test_normal; } } } return found || ''; } function index_after_formatting(position, command) { var start = position === 0 ? 0 : position - 1; var command_len = length(command); for (var i = start; i < command_len; ++i) { var substr = command.substring(0, i); var next_substr = command.substring(0, i + 1); var formatted_substr = replace_function(substr); var formatted_next = replace_function(next_substr); var substr_len = length(formatted_substr); var next_len = length(formatted_next); var test_diff = Math.abs(next_len - substr_len); if (test_diff > 1) { console.log('return ' + i); return i; } } } return function get_formatted_position(position, command) { var formatted_position = position; var string = replace_function(command); var len = length(string); var command_len = length(command); if (len !== command_len) { var orig_sub = command.substring(0, position); var orig_len = length(orig_sub); var sub = replace_function(orig_sub); var sub_len = length(sub); var diff = Math.abs(orig_len - sub_len); if (false && orig_len > sub_len) { formatted_position -= diff; } else if (false && orig_len < sub_len) { formatted_position += diff; } else { var index = index_after_formatting(position, command); var to_end = command.substring(0, index + 1);
<textarea>xxxx :smile: xxxx :smile: xxx :smile:</textarea> <pre></pre>
javascript string
jcubic
source share