1

I have object which i have converted using JSON.stingrify

{
  "_originalElementInner": "            <input type=\"file\" name=\"\" class=\"hidden\" id=\"file_input_two\" onchange=\"alertFilename(this.value,'cropped_two');\">                           <!-- <img src=\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\" alt=\"\" width=\"100%\" height=\"100%\"> -->                                         "
}

From above i want to fetch "cropped_two", which is on

onchange=\"alertFilename(this.value,'cropped_two');

Here this.value will remain same, so if in case want to split the string .

I have tried via multiple ways by looping and other but in vain.

Anyone can help ? Thanks in advance.

8
  • 1
    I think you are going to have to try to restate your question. It's not clear what you are attempting to do. Commented Oct 31, 2018 at 1:57
  • What i really want to do is, I have that array above which is having HTML inside. Now from that array of HTML string, i want to get 2nd value of onchange=\"alertFilename(this.value,'cropped_two'); i.e. 'cropped_two' I want to get this value. Commented Oct 31, 2018 at 1:58
  • Will this be executed through a browser? and is it possible to use jQuery? Commented Oct 31, 2018 at 1:59
  • what cropped_two is supposed to mean? Commented Oct 31, 2018 at 2:01
  • Yes. Javascript Commented Oct 31, 2018 at 2:01

4 Answers 4

1

I think you just want the string? I guess this would work.

let o = {
  "_originalElementInner": "            <input type=\"file\" name=\"\" class=\"hidden\" id=\"file_input_two\" onchange=\"alertFilename(this.value,'cropped_two');\">                           <!-- <img src=\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\" alt=\"\" width=\"100%\" height=\"100%\"> -->                                         "
};
const regex = /,(.*)\);/gm;
const match = regex.exec(o._originalElementInner);
console.log(match[1]);

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

Comments

0

you can use a search function to find "this.value" position, then use substring function from "this.value" position + 11 to the position of ")" witch seems to be the only ")" in the string.

Comments

0

If it is possible to use jQuery, you can follow this approach.

var obj = {"_originalElementInner": "            <input type=\"file\" name=\"\" class=\"hidden\" id=\"file_input_two\" onchange=\"alertFilename(this.value,'cropped_two');\">                           <!-- <img src=\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\" alt=\"\" width=\"100%\" height=\"100%\"> -->                                 "},
    stringValue = '',
    // This function will be called when the event change is triggered.
    alertFilename = function(_, str) { stringValue = str;};

// Create the jQuery object and trigger the event change
// Finally remove the function from window object (This is just to remove an unsed function after execute it).
$(obj['_originalElementInner']).trigger('change'), (delete window['alertFilename']);

console.log(stringValue);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

Thank you Ele, this also works but Will's answer is simple javascript :) but this is also new for me. :)
0

If there could be several onchange callbacks in your code, the following code will get the one that refers to alertFilename, it is also faster than regexp.

function extractSecondArg(text) {
  // Define the pattern to look for.
  const pattern = 'onchange=\"alertFilename(this.value,'

  // Find the index of the pattern.
  const patternIndex = text.indexOf(pattern);

  if (patternIndex !== -1) {
    // Find the closing parenthesis.
    const patternEnd = ');';
    const patternEndIndex = text.indexOf(patternEnd, patternIndex);

    // Extract the second argument of the function.
    return text.substring(patternIndex + pattern.length, patternEndIndex).replace(/["']+/g, '');
  }
}

Here is an example of how to use it.

const json = {
  "_originalElementInner": "            <input type=\"file\" name=\"\" class=\"hidden\" id=\"file_input_two\" onchange=\"alertFilename(this.value,'cropped_two');\">                           <!-- <img src=\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\" alt=\"\" width=\"100%\" height=\"100%\"> -->                                         "
};

// Extract element to search in.
const secondArg = extractSecondArg(json['_originalElementInner']);
console.log(secondArg)

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.