Customize the regular expression of the Name fields for any field type of the content-type

System Information
  • Strapi Version: 3.6.8
  • Operating System: macOS Big Sur 11.4
  • Database: mysql Ver 8.0.26 for macos11.3 on x86_64 (Homebrew)
  • Node Version: v14.17.4
  • NPM Version: 6.14.14
  • Yarn Version: 1.22.11

Hello,

When I created field Strapi constraints the Name to match the regex ([A-Za-z0-9_]), and I want to customize this regex so that can use colon character “:” in the name of fields (for example xmlns:S) by using this regex [\w]+:?[\w]+.
Where I can do this customization in the code? (I didn’t find something related to this in the documentation),
Or if there is another way to do this that would be amazing.

Thanks in advance.

Hi there @Leena i think you can under advanced settings use a regex like this

^[a-zA-Z]+:[a-zA-Z]$

This will allow A-Z and a-z a : and then same a-z or A-Z I’m guessing you can replace the A-Z with numbers as well or add to it. This will match the whole line as well due the ^

Thanks @Eventyret this will help, but I didn’t get the answer to my question…

I’m so sorry I might have missed it then, are you trying to add a regex to the “name” property or what are you trying to do then? :slight_smile: @Leena

It’s not possible to change the regex for that field as it’s a programmatic field used for the keys in the database, REST, GraphQL, ect.

After you create the field in the content-type and save it, you can use the Content-Type Manager to change the view in the Admin: Configuring views of a content type - Strapi User Guide

I see…

Actually I want to change the name field for content when I fetch the data, not the view.

Thanks for responding.

Yeah for something like that you can’t use special characters, it needs to be a normalized string that follows that regex exactly. Any other format will be subject to errors or unexpected results.

Especially with how we construct the database and the internal API

2 Likes

Wasn’t aware of that thanks @DMehaffy :pray: :grinning_face_with_smiling_eyes: