0

これが都市名のJavaScript正規表現で、これを除くほとんどすべてのケースを処理しています。

^[a-zA-Z]+[\. - ']?(?:[\s-][a-zA-Z]+)*$

(合格する必要があります)

  • コーダレーン
  • サンタンバレー
  • セントトーマス
  • セントトーマス-ヴィンセント
  • セントトーマスビンセント
  • セントトーマス-ヴィンセント
  • セントトーマス
  • アナコンダ-ディアロッジ郡

(失敗するはずです)

  • さん。タン。谷
  • セントトーマス
  • セントトーマス--ヴィンセント
  • セントトーマス-ヴィンセント
  • セント-トーマス
4

3 に答える 3

1

これは、最初のリストのすべての名前と一致し、2番目のリストの名前とは一致しません。

/^[a-zA-Z]+(?:\.(?!-))?(?:[\s-](?:[a-z]+')?[a-zA-Z]+)*$/

複数行の説明:

^[a-zA-Z]+     # begins with a word
(?:\.(?!-))?   # maybe a dot but not followed by a dash
(?:
 [\s-]         # whitespace or dash
 (?:[a-z]+\')? # maybe a lowercase-word and an apostrophe
 [a-zA-Z]+     # word
)*$            # repeated to the end

ドットをどこにでも許可するには、ドットを2つではなく、次のように使用します。

/^(?!.*?\..*?\.)[a-zA-Z]+(?:(?:\.\s?|\s|-)(?:[a-z]+')?[a-zA-Z]+)*$/

^(?!.*?\..*?\.) # does not contain two dots
[a-zA-Z]+       # a word
(?:
 (?:\.\s?|\s|-) # delimiter: dot with maybe whitespace, whitespace or dash
 (?:[a-z]+\')?  # maybe a lowercase-word and an apostrophe
 [a-zA-Z]+      # word
)*$             # repeated to the end
于 2013-01-28T15:39:40.223 に答える
0

次の正規表現はあなたの要件に合うと思います:

^([Ss]t\. |[a-zA-Z ]|\['-](?:[^-']))+$

一方、正規表現を使用してそれを行うというアイデアに疑問を呈するかもしれません...正規表現の複雑さに関係なく、一致する新しい不要なパターンを見つけるのは常に愚か者です...

通常、有効な都市名が必要な場合は、googlegeocodingAPIなどのジオコーディングAPIを使用することをお勧めします

于 2013-01-28T15:38:02.523 に答える
0

この正規表現を試してください:

^(?:[a-zA-Z]+(?:[.'\-,])?\s?)+$

これは一致します:

コーダレーン
サンタンバレー
セントトーマス
セントトーマス-ヴィンセント
セントトーマスヴィンセント
セントトーマス-ヴィンセント
セント-トーマス
アナコンダ-ディアロッジ郡
モンテセントトーマス
サン。タン。バレー
ワシントンDC

しかし、一致しません:

セントトーマス
セントトーマス--ヴィンセントセント-
トーマス-ヴィンセント
セント--トーマス

San. Tan. Valley(おそらく2つのピリオドを持つ都市名があるので、一致させました。)

正規表現の仕組み:

# ^         - Match the line start.
# (?:       - Start a non-catching group
# [a-zA-Z]+ - That starts with 1 or more letters.
# [.'\-,]?  - Followed by one period, apostrophe dash, or comma. (optional)
# \s?       - Followed by a space (optional)
# )+        - End of the group, match at least one or more of the previous group.
# $         - Match the end of the line
于 2013-01-29T07:50:50.160 に答える