locked
regex to capitalize specific letters RRS feed

  • Question

  • User-809753952 posted

    Hi I need a regex solution for the following problem.

    An address line text sometimes ends with the characters (se, sw,ne or nw.)  

    I want to capitalize them. 

    Example:

    var inputStr = "43a peach street ne";

    Output must be  43a Peach Street NE

    Please help

    Monday, October 19, 2020 1:41 PM

Answers

  • User1535942433 posted

    Hi mnmhemaj,

    Accroding to your description,could you tell us what's regex?Do you need to use regex expression in javascript to capitalize letters?

    And could you tell us whatever the last word, all capitals?Or just se,sw,ne,nw?

    Since you don't post your codes,I create a test.

    function capital_letter() {
                var str = "43a peach street ne";
                var result = str.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase());
                result = result.split(" ");
                for (var i = 0, x = result.length; i < x; i++) {
                    if (i == x - 1) {
                        if (result[i] == "Se" || result[i] == "Sw" || result[i] == "Ne" || result[i] == "Nw") {
                            result[i] = result[i].replace(/\w+/g, match => match.toUpperCase())
                        }
                    }
                }
                return result.join(" ");
            }

    By the way,do you must use regex?I think,bellow this is more convenient:

     str[i] = str[i][0].toUpperCase() + str[i].substr(1);

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2020 6:12 AM

All replies

  • User1535942433 posted

    Hi mnmhemaj,

    Accroding to your description,could you tell us what's regex?Do you need to use regex expression in javascript to capitalize letters?

    And could you tell us whatever the last word, all capitals?Or just se,sw,ne,nw?

    Since you don't post your codes,I create a test.

    function capital_letter() {
                var str = "43a peach street ne";
                var result = str.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase());
                result = result.split(" ");
                for (var i = 0, x = result.length; i < x; i++) {
                    if (i == x - 1) {
                        if (result[i] == "Se" || result[i] == "Sw" || result[i] == "Ne" || result[i] == "Nw") {
                            result[i] = result[i].replace(/\w+/g, match => match.toUpperCase())
                        }
                    }
                }
                return result.join(" ");
            }

    By the way,do you must use regex?I think,bellow this is more convenient:

     str[i] = str[i][0].toUpperCase() + str[i].substr(1);

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2020 6:12 AM
  • User-809753952 posted

    Thank you so much yij. It works. 

    Tuesday, October 20, 2020 9:29 AM