0

I want to automatically correct the proper format of IDs found on a website:

So im detecting if a specific digit-number is present in a website with

if (window.location.href.match(/\d{10}/)) {  alert('ID detected');} else {  alert('does not contain an ID');}

e.g raw ID: "1234567899"

now i want to add a minus "-" to it for it to show like likes "123-456-7899"

The minus has to be put in 1x after the first 3 digits and 1 x after the firs 6 digits

I am struggling to find a proper code for this, does anybody might have an idea on how to make this work?

  • Something like `s[:3] + '-' + s[3:6] + '-' + s[6:]` ? (in Python syntax) –  Apr 19 '22 at 10:08
  • 4
    Does this answer your question? [Insert hyphens in javascript](https://stackoverflow.com/questions/6981487/insert-hyphens-in-javascript) – Ivar Apr 19 '22 at 10:11
  • Thank you! I have found this in javascript: `s.slice(0, 3) + "-" + s.slice(3, 6) + "-" + s.slice(6);` but it does not work, maybe because its not fetching and saving the ID to slice? – bilatoor Apr 19 '22 at 10:16
  • This ID is in the location pathname or the query string? And you want to format the number for display or use in your code? The question isn't very clear. – Yogi Apr 19 '22 at 10:27
  • Please apologize. The ID is not supposed to be in the pathname, its supposed to be found in a webpage (text) – bilatoor Apr 19 '22 at 11:14

1 Answers1

0

since its a string you can concatenate substrings with - to form your desired output

const id = "1234567899";
const idDashed = `${id.substring(0,3)}-${id.substring(3,6)}-${id.substring(6,id.length)}`;
console.log(idDashed)
//"123-456-7899"
ahsan
  • 1,409
  • 8
  • 11
  • 1
    seems to work for the described problem don't know why this response was downvoted – adiian Apr 19 '22 at 10:20
  • [Please don't answer duplicate questions](https://stackoverflow.com/help/how-to-answer). Add it to the duplicate if you want to. (Though it is already very similar to [this answer](https://stackoverflow.com/a/6981548/479156).) – Ivar Apr 19 '22 at 10:21
  • @adiian Votes are to rate the quality of an answer. Answers that work can still be of poor quality or not useful. – Ivar Apr 19 '22 at 10:22
  • makes sense, I flagged the question as duplicate, seemed unfair to downvote the only valid answer without an explanation – adiian Apr 19 '22 at 10:28
  • 1
    @adiian In the past it happened too often that people who explained downvotes got revenge-downvoted or even off-site harassed due to people not agreeing with them. As a result it is now advised **not** to explain downvotes anymore. (You can however still leave a comment explaining what is wrong with a post. Just don't refence the downvote.) – Ivar Apr 19 '22 at 10:34
  • Thank you very much Adiian! I see that this would require a defined "const" `const id = "1234567899";` however i am trying to scan the website for the numbers so often the amount of IDs detected would be unpredictable if you take a look at my code above.. – bilatoor Apr 19 '22 at 10:46
  • i found this "replace(/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/, '$1$2$3-$4$5$6-$7$8$9$10')" but its not applying – bilatoor Apr 19 '22 at 11:01