Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

can i give a string for in place of regExp in QML



  • I tried doing something like this in QML and it wasnt allowed
    Item{
    property string reg_expression: "/[0-9]+/"
    ...
    ..
    .
    RegExpValidator {id:decimal_validator ; regExp: reg_expression}

    is there a way to give regExp in regExpValidator a string?
    because i want to send the regExp from the backend as a string to the QML side.

    Is this possible?



  • Guess you can give the string to RegExp constructor

    RegExpValidator {id:decimal_validator ; regExp: new RegExp(reg_expression)}
    

    Edit: Another point, I think you don't need to add extra "/" character if you define your regex using string.
    I think it should be either :

    property string reg_expression: "[0-9]+"
    

    or

    property RegExp reg_expression: /[0-9]+/
    

    Note also you can use "\\d+" or /\d/ instead of "[0-9]+" for your regex

    You may also be interested by this great website for developing and testing regex: https://regex101.com

    Edit2: Correction of my stupid mistake of my first edit. regex for digit character is obviously \d and not \w



  • Guess you can give the string to RegExp constructor

    RegExpValidator {id:decimal_validator ; regExp: new RegExp(reg_expression)}
    

    Edit: Another point, I think you don't need to add extra "/" character if you define your regex using string.
    I think it should be either :

    property string reg_expression: "[0-9]+"
    

    or

    property RegExp reg_expression: /[0-9]+/
    

    Note also you can use "\\d+" or /\d/ instead of "[0-9]+" for your regex

    You may also be interested by this great website for developing and testing regex: https://regex101.com

    Edit2: Correction of my stupid mistake of my first edit. regex for digit character is obviously \d and not \w


Log in to reply