# Regex - insert a space - but only once!

**URL:** <https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610>\
**Category:** General\
**Created:** [1 February 2022 23:01 UTC](https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610 "2022-02-01T23:01:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [1 February 2022 23:01 UTC](https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610/1 "2022-02-01T23:01:58Z")

</div>

Anyone know how to modify this regex to insert a space into a UK phone number but only do it once after the 5 digits and not again at 10 digits?

```auto
msg.payload = msg.payload.replace(/(.{5})/g,'$1 ');
return msg;

```

thanks!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 February 2022 23:17 UTC](https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610/2 "2022-02-01T23:17:16Z")

</div>

Remove the `g`

> Definition and Usage  
> The "g" modifier specifies a global match.  
> A global match finds all matches (compared to only the first).

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 February 2022 23:23 UTC](https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610/3 "2022-02-01T23:23:09Z")

</div>

You could, of course, do that without a regex by splitting the string and rejoining with a space character.

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [1 February 2022 23:28 UTC](https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610/4 "2022-02-01T23:28:08Z")

</div>

thanks both!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 April 2022 23:28 UTC](https://discourse.nodered.org/t/regex-insert-a-space-but-only-once/57610/5 "2022-04-02T23:28:56Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
