0
<script language="javascript" type="text/javascript">
function textremv() {
    var output = document.getElementById("content").value;
    output = output.replace(/\s/g, "") 
    output = output.replace(/-/g, "")
    output = output.replace(/,/g, ", ")
    output = output2
    output2 = Array.from(new Set(output2.split(','))).toString();
    document.getElementById("output").innerHTML = output;
}
</script>


<textarea rows='5' cols='50' id='content'></textarea>
<input type='button' value='Extract Text' onclick='textremv()'/>
<div id="output"></div>

Purpose: Aiming to break down phone numbers to 10 characters by first removing extra spaces, removing dashes, and then adding a space after the comma.

Ex: 555- 555-5555, 555-5555555, 555555-5555 in a textarea would return 5555555555, 5555555555, 5555555555

GOAL: The code above works kind of if I remove "output = output2." I was trying to save the "fixes" into a new var. What I want to do is remove duplicates from the output which the code above is removing duplicates prior to the format. So the only return I would get would just be 5555555555 since there are duplicates in that example.

I'm not married to the above code if there's another way in javascript. I did research other questions on here but what I'm finding is they don't really mesh up with the output that I'm using.

EDIT: T.J's answer worked for me. I also made another textarea and added this

document.getElementById("content2").innerHTML = output;

where content2 is the name of another text area incase anyone else finds this and wants to have an easy copy/paste option.

1 Answer 1

1

The code above works kind of if I remove "output = output2." I was trying to save the "fixes" into a new var.

That's because output = output2 replaces the string you just constructed with whatever was originally in output2.

I'm not sure where output2 comes from (it's not declared in your code), so I'm adding a let to declare it in the following:

var output = document.getElementById("content").value;
let output2 = output.replace(/\s/g, "");
output2 = output2.replace(/-/g, "");
output2 = output2.replace(/,/g, ", ");
output2 = Array.from(new Set(output2.split(','))).toString();
document.getElementById("output").innerHTML = output2;

Though there's really no need for output2 at all, and you can combine things a bit:

let output = Array.from(
    new Set(
        document.getElementById("content").value
        .replace(/\s|-/g, "")
        .split(',')
    )
).join(", ");
document.getElementById("output").innerHTML = output;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This helped and I figured out how to make it write to another textarea. Thank you TJ

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.